JQuery.validator.addMethod Custom Validation

Source: Internet
Author: User

JQuery.validator.addMethod ("Numorletter", function (value, Element) {
return this.optional (Element) | | /^[0-9| A-z|a-z]+$/.test (value);
}, "Please enter a number or letter");

$ ("#inputForm"). Validate ({
Rules: {
The name of the form to validate
Operationcode: {
Custom validation Rules
Numorletter:true,
Ajax check, through the background service to verify whether the user's lost content exists in the database, background service only need to return True or False, Boolean type, String type can be
remote:{
Here by get method, the parameter is placed after the URL, just start? After writing the operationcode=, after the request is sent, the URL becomes? operationcode=&operationcode=value
So I got rid of it.
URL: "${ctx}/operation/operation/validatecode?" + $ ("#operationCode"). Val (),
Type: "GET",
DataType: "JSON"
Post mode, you can use the
Data:{paramname:value,...} Passing parameters
}
}
},
messages:{
operationcode:{
This specifies the error message
Remote: "The specified number already exists"
}
}
});


Ajax checksum, you can use your own remote, or you can add it via Addmethod ()


JQuery.validator.addMethod ("Checkunique", function (value, Element) {
var deferred = $. Deferred ();//Create a lingering object
$.ajax ({
URL: "${ctx}/operation/operation/listjson?operationcode=" +value,
async:false,//to specify that it cannot be asynchronous, you must wait for the background service validation to complete before executing subsequent code
DataType: "JSON",
Success:function (page) {
var len = page.rows.length;
if (Len > 0) {
Deferred.reject ();
} else {
Deferred.resolve ();
}
}
});
Deferred.state () has 3 states: pending: Not yet finished, rejected: failed, Resolved: Success
return deferred.state () = = "Resolved"? True:false;
}, "number already exists");


The benefits of the AJAX checksum remote that is added via Addmethod ():
The service returned by the remote () request must have a Boolean or string type of true or false, so the backend must add such a service if it is to be validated
Addmethod (), using $.ajax () can process the data returned by the service in success, so that the background work (return true,false) is transferred to the foreground.

JQuery.validator.addMethod Custom Validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.