In my opinion, warning boxes are only used when the browser does not support other methods of displaying error messages. The W3C recommends that we display error messages near form items. This is a good approach, so we only use warning dialogs when the browser does not support this advanced approach.
Example
Try the example below. Each item is required. Also I would insist if the email item has the @ symbol. If not, an error message will be prompted.
var x = document.forms[0].elements;
for (var i=0;i if (!x[i].value)
writeError(x[i],'This field is required');
}
if (x['email'].value.indexOf('@') == -1)
writeError(x['email'],'This is not a valid email address');
The core of this vlidate() function is the same as usual. Check for errors in any order you want. When you find an error, call writeError() and pass it the error field and error message.
If the browser does not support W3C DOM, use errorString to generate a warning box. You may want to modify the content of the warning box.
if (!W3CDOM)
alert(errorstring) ;
For user convenience, set focus on the first error element.
Finally return validaForm. If it is still true, submit the form. If not, stop submitting.
writeError()
This function is used to output error information to the form item. If it fails, it means that the browser does not support W3C DOM, and then the error message is sent to errorstring.
This function will pass a form item and an error message.
function writeError(obj,message)
{
First we set validForm to false: this form is not filled in correctly and should not be submitted.
validForm = false;
}
Then check whether the form item already has an error message. If so, return to the validation() function. I don't want to have two error messages after the same item.
if (obj.hasError) return; Check whether the browser supports W3C DOM:
obj.className = ' error';
Then set an onchange event handler for the error form item:
obj.onchange = removeError;
Create a
to contain the error message, and Set its class to "error". Set the style to be rendered in CSS.
var sp = document.createElement('span');
sp.className = 'error';
Add an error message text node to .
sp.appendChild(document.createTextNode(message));
Then add this to the corresponding form item (in this example, each form item has a tag).
obj.parentNode.appendChild(sp);
Finally, set the hasError attribute to this form. This attribute can be used both to indicate the incorrect form item and to facilitate removal of the error message in the future.
obj.hasError = sp; 2 }
For For browsers that do not support it, we save the name of the form item and the error message in the errorstring. This string will pop up at the end. Also set the hasError attribute for him.
[code] else {
errorstring = obj.name ': ' message 'n';
obj.hasError = true;
}
If the value of validForm is still true at this time, set firstError to the current element. to facilitate setting focus in the future.
if (validForm)
firstError = obj;
removeError()
The onchange event handler of each error form item points to this function. If the user modifies the corresponding form field, we politely assume that the error has been fixed. So the error message should disappear.
First remove the error item in the form item's class. This is used to remove specific error styles.
function removeError() {
this.className = this.className.substring(0,this.className.lastIndexOf(' '));
Then remove the error message. The hasError attribute points to the that contains the information, so we remove it from the form item's parent element.
this.parentNode.removeChild(this.hasError);
Finally do some cleaning. Set the hasError property to null, and then remove the onchange event handler.
[code]this.hasError = null;
this.onchange = null;
}
Translation address: http://www.quirksmode .org/dom/error.html
Please keep the following information for reprinting
Author: Beiyu (tw:@rehawk)