Jquery.validate Remote and custom authentication methods
$ (function () {
var validator = $ ("#enterRegForm"). Validate ({
Debug:false,//debug mode Cancel the default submit feature of Submit
Errorclass: "Error",//The default is the wrong style class: Error
Validclass: "Check",//Verify successful style, default string valid
focusinvalid:true,//when a form is submitted, the focus points to the first domain that does not pass validation
focuscleanup:true;//when the focus points to the wrong field, hide the error message and not use it with Focusinvalid!
Onkeyup:true,
Errorelement: "Div",
Submithandler:function (form) {//form submit handle, as a callback function, with one parameter: form
Form.submit (); Submit Form
},
Rules: {
"Enterprise.enname": {
Required:true,
Minlength:6,
remote:{
URL: "/nameservlet",//Background handler
Type: "Get",//Data sending method
DataType: "JSON",//Accept data format
Data: {//To be passed
Enname:function () {
Return $ ("#enName"). Val ();
}
}
}
},
"User.password": {
Required:true,
RANGELENGTH:[6,18]
},
passwordconf:{
Required:true,
RANGELENGTH:[6,18],
Equalto: "#passWord"//PassWord here is <input id= "PassWord" > at first thought it was the value of name, and the gas was dead.
}
},
Messages: {//custom validation message
"Enterprise.enname": {
Required: "Please fill in the Enterprise name!" ",
MinLength: $.format ("At least {0} characters!") "),
Remote:$.format ("The Enterprise name already exists!") ")
},
"User.password": {
Required: "Please fill in the Confirmation password!" ",
Rangelength:$.format ("Password to be between {0}-{1} characters!") ")
},
passwordconf:{
Required: "Please fill in the Confirmation password!" ",
Rangelength:$.format ("Confirm password between {0}-{1} characters!") "),
Equalto: "Confirm the password to match the password!" "
},
Errorplacement:function (Error, Element) {//Verify where message is placed
Error.appendto (Element.parent ("TD"). Next ("TD"). Children (". Msg"));
Error.appendto (Element.parent (". Field"). Next ("div"));
},
Highlight:function (element, Errorclass) {//Set highlighting for validated forms
$ (Element). addclass (Errorclass);
},
Success:function (Div) {
Div.addclass ("valid");
}
});
});
Custom methods;
Create a new JS file: $ (document). Ready (function () {
Minimum character length verification (one Chinese characters length is 2)
JQuery.validator.addMethod ("Stringminlength", 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);
}, $.validator.format ("length cannot be less than {0}!"));
Character Maximum length verification (one Chinese characters length is 2)
JQuery.validator.addMethod ("Stringmaxlength", 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);
}, $.validator.format ("length cannot be greater than {0}!"));
Character verification
JQuery.validator.addMethod ("Stringcheck", function (value, Element) {
return this.optional (Element) | | /^[\u0391-\uffe5\w]+$/.test (value);
}, "can only include Chinese characters, English letters, numbers and underscores";
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 the value entered is between 3-15 bytes (2 bytes in one text)");
//character validation
JQuery.validator.addMethod ("string", function (value, Element) {
return this.optional (Element) | |/^[\u0391-\uffe5 \w]+$/.test (value);
}, "Do not allow special symbols to be included!");
//must begin validation with a specific string
JQuery.validator.addMethod ("Begin", function (value, element, param) {
var begin = New RegExp ( "^" + param);
return this.optional (element) | | (Begin.test (value));
}, $.validator.format ("must start with {0}!"));
//Verify that the input values are not the same two times
JQuery.validator.addMethod ("Notequalto", function (value, element, param) {
return value! = $ (param). Val ();
}, $.validator.format ("two times input cannot be the same!");
//Verify value is not allowed with a specific value equal to
JQuery.validator.addMethod ("NotEqual", function (value, element, param) {
return value! = Param
}, $.validator.format ("input value not allowed for {0}!"));
//validation value must be greater than a specific value (cannot be equal to
JQuery.validator.addMethod ("GT", function (value, element, param) {
return value > param;
}, $. Validator.format ("Input value must be greater than {0}!"));
var decimal =/^-?\d+ (\.\d{1,2})? $/;
return this.optional (Element) | | (Decimal.test (value));
}, $.validator.format ("No more than two decimal digits!"));
//alpha-numeric
JQuery.validator.addMethod ("Alnum", function (value, Element) {
return this.optional (Element) | |/^[ A-za-z0-9]+$/.test (value);
}, "can only include English letters and Numbers");
//Kanji
JQuery.validator.addMethod ("Chcharacter", function (value, Element) {
var tel =/^[\u4e00-\u9fa5]+$/;
return this.optional (Element) | | (Tel.test (value));
}, "Please enter Chinese characters");
//ID number verification
JQuery.validator.addMethod ("Isidcardno", function (value, Element) {
Return this.optional ( Element) | | /^[1-9]\d{7} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3}$/.test (value) | | /^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) ((\d{4}) |\d{3}[a-z]) $/.test (value);
}, "Please enter your ID number correctly");
Mobile phone number Verification
JQuery.validator.addMethod ("IsMobile", function (value, Element) {
var length = Value.length;
var mobile =/^ ((13[0-9]{1}) | ( 15[0-9]{1})) +\d{8}) $/;
return this.optional (Element) | | (length = = && Mobile.test (value));
}, "Please TB correctly fill in your mobile phone number");
Phone number Verification
JQuery.validator.addMethod ("Istel", function (value, Element) {
var tel =/^\d{3,4}-?\d{7,9}$/; Phone number format 010-12345678
return this.optional (Element) | | (Tel.test (value));
}, "Please fill in your phone number correctly");
Contact Phone (phone/Phone) verification
JQuery.validator.addMethod ("Isphone", function (value,element) {
var length = Value.length;
var mobile =/^ ((13[0-9]{1}) | ( 15[0-9]{1})) +\d{8}) $/;
var tel =/^\d{3,4}-?\d{7,9}$/;
return this.optional (Element) | | (tel.test (value) | | mobile.test (value));
}, "Please fill in your contact number correctly");
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");
});
The file must be referenced before the form is validated.
Note: The URL in remote is a servlet, but how does the servlet write it?
At first I thought it was the same as usual: Out.println ("username" +name+ "already exists!") ");
Although it can be verified, there is still a problem: Enter a name that does not exist or a hint exists.
Later, Google, see others have made such a mistake, the solution to return True,false.
But doget is of type void, and return true is not feasible.
Later asked the master in the group, the correct wording is out.println ("true");
Hey, it's been a long time.
Finally, the problem is solved.
Posted on 2012-08-15 15:02 chen11-1 Read (5626) Comments (0) Edit Collection