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