Form validation is essential for almost every website that needs to be registered or logged in. Some of the validation is very complex, it can be said that a variety of requirements for you, but this chapter only describes the simplest form of validation, is to determine whether the empty, some of the requirements of the lower Web site has been satisfied with the need.
The code is as follows:
The above code, when clicked the Submit button, can carry on the simple form verification, if the form item is empty, then will pop-up the prompt, and will focus in the current form item, the code is relatively simple, here the distance does not have many introduction, may refer to the related reading.
Here is a look at the following JS validation form instance code:
Gspan.html
check.js
function Gspan (cobj) {//Get the span label after the form to display the hint information if (cobj.nextSibling.nodeName!= ' span ') {Gspan (cobj.nextsibling);
else {return cobj.nextsibling;}} Check the form obj "Form object", info "prompt" fun "handle function" click "needs to be clicked, the commit need to trigger" function check (obj, info, fun, click) {var sp = Gspan (ob
j);
Obj.onfocus = function () {sp.innerhtml = info;
Sp.classname = ' Stats '; } Obj.onblur = function () {if (Fun (This.value)) {sp.innerhtml = "input correct!"
";
Sp.classname = "Stats";
else {sp.innerhtml = info; sp.classname = "Stats";}}
if (click = = ' click ') {Obj.onblur ();}} onload = regs; Page load Execution function regs (click) {var stat = true;//return status, use username = document.getelementsbyname (' username ') when submitting data []; pass
Word = document.getelementsbyname (' password ') [];
Chkpass = Document.getelementsbyname (' Chkpass ') [];
email = document.getelementsbyname (' email ') []; Check (username, "length of username in-between", function (val) {if Val.match (/^\s+$/) && val.length >= && Val.length & lt;=) {return true;} else {stat = false; retUrn false;
}, click); Check (password, "Password must be between-bit", function (val) {if Val.match (/^\s+$/) && val.length >= && val.length
<=) {return true;} else {stat = false; return false;}}, click); Check (Chkpass, "Make sure the password is consistent with the above, the rules are the same", function (val) {if (Val.match (/^\s+$/) && val.length >= &&
Val.length <= && val = = Password.value) {return true;} else {stat = false; return false;}, click);
Check (email, "Enter by mailbox Rule", function (val) {if (Val.match (/\w+@\w+\.\w/)) {return true;} else {stat = false; return false;}
}, click);
return stat; }