Article Introduction: easy to use jquery form verification plug-in--jquery.validate.js. |
Jquery.validate.js is a validation plug-in for jquery, and with the advantage of jquery, we can quickly validate some of the common inputs and expand our own authentication methods. Please download the necessary jquery plugins before using: Jquery-1.4.2.min.js and Jquery.validate.min.js.
The following shows how to use the Jquery.validate.js plug-in to perform validation of forms.
This is an HTML form:
User name:
Password:
Confirm Password:
questions:
Answer:
real name:
Certificate number:
Mobile:
Tel:
e-mail:
Zip:
This is my form validation code:
$ (document). Ready (function () {
$.validator.setdefaults ({
Submithandler:function (form) {
Form.submit ();
}
});
Two bytes in text
JQuery.validator.addMethod ("Byterangelength", function (value, element,
param) {
var length = Value.length;
for (var i = 0; i < value.length; i++) {
if (Value.charcodeat (i) > 127) {
length++;
}
}
return this.optional (Element)
(length >= param[0] && length <= param[1]);
}, " Make sure that the value entered is between 3-15 bytes (2 bytes in one) ");
Whether the user name exists
JQuery.validator.addMethod ("Isusernameexist", function (value, Element) {
var flag = $.ajax ({
Type: "POST",
URL: "Validateservlet",
Data: "Username=" + value,
Async:false,
Success:function (msg) {
Alert ("Return Data:" + msg);
}
}). responsetext;
return this.optional (Element) flag = = 1;
}, " The user does not exist ");
ID Card number Verification
JQuery.validator.addMethod ("Isidcardno", function (value, Element) {
return this.optional (Element) Isidcardno (value);
}, " Please enter your ID number correctly ");
Character validation
JQuery.validator.addMethod ("UserName", function (value, Element) {
return this.optional (Element)
/^[\u0391-\uffe5\w]+$/.test (value);
" username can only include Chinese characters, English letters, numbers and underscores ");
Mobile phone number Verification
JQuery.validator.addMethod ("IsMobile", function (value, Element) {
var length = Value.length;
return this.optional (Element)
(length = = &&/^ ((13[0-9]{1}) (15[0-9]{1}) +\d{8}) $/
. Test (value));
}, " Please fill in your mobile phone number ");
Phone number Verification
JQuery.validator.addMethod ("Isphone", function (value, Element) {
var tel =/^ (\d{3,4}-?)? \d{7,9}$/g;
return this.optional (Element) (Tel.test (value));
}, " Please fill in your phone number ");
ZIP Code Verification
JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) (Tel.test (value));
}, " Please fill in your zip code " correctly;
$ (regfrom). Validate ({
Rules: {
UserName: {
Required:true,
Username:true,
Byterangelength: [3, 15],
Isusernameexist:true
},
Password: {
Required:true
},
Repassword: {
Required:true,
Equalto: "#password"
},
Question: {
Required:true
},
Answer: {
Required:true
},
Realname: {
Required:true
},
Cardnumber: {
Isidcardno:true
},
Mobilephone: {
Ismobile:true
},
Phone: {
Isphone:true
},
Email: {
Required:true,
Email:true
},
ZipCode: {
Iszipcode:true
}
},
Messages: {
UserName: {
Required: " Please fill in username ",
Byterangelength: " username must be between 3-15 characters (2 characters in a literal) ",
Isusernameexist: " The user does not exist "
},
Password: {
Required: " Please fill in the Password ",
Minlength:jquery
. Format (" enter at least {0} characters .")
},
Repassword: {
Required: " Please fill in the confirmation password webjx",
Equalto: " two times password input is not the same ",
Minlength:jquery
. Format (" enter at least {0} characters .")
},
Question: {
Required: " Please fill in your password hint question "
},
Answer: {
Required: " Please fill in your password hint answer "
},
Realname: {
Required: " Please fill in your real name "
},
Email: {
Required: " Please enter an email address ",
Email: " Please enter a valid email address "
}
},
Errorplacement:function (Error, Element) {
Error.appendto (Element.parent ());
},
Success:function (label) {
Set as text for IE
Label.html ("OK!");
},
Focusinvalid:false,
Onkeyup:false
});
When the input box gets the focus, the style setting
$ (' input '). focus (function () {
if ($ (this). are (": Text") $ (this). Is (":p assword")
$ (this). addclass (' Focus ');
if ($ (this). Hasclass (' Have_tooltip ')) {
$ (this). Parent (). Parent (). Removeclass (' Field_normal ')
. addclass (' Field_focus ');
}
});
Style settings when the input box loses focus
$ (' input '). blur (function () {
$ (this). Removeclass (' Focus ');
if ($ (this). Hasclass (' Have_tooltip ')) {
$ (this). Parent (). Parent (). Removeclass (' Field_focus ')
. addclass (' Field_normal ');
}
});
});