$ (document). Ready (function() {JQuery.validator.addMethod ("Namerepeate",function(value, Element) {//Use the jquery Ajax method to verify that the customer name already exists varFlag =false; $.ajax ({type:"POST", URL:' @Url. Action ("Testclientnameisrepeat", "Clientext", new {area = "Client"}) ', Async:false,//synchronous method, if used asynchronously, flag is always 1data:{' name ': $ ("#Name"). Val (), ' ClientId ': $ ("#ClientID"). Val ()}, Success:function(data) {flag=data. Isnamerepeate; } }); return!Flag; }, "The customer name already exists in the customer data and is not added repeatedly." "); JQuery.validator.addMethod ("Deptrepeate",function(value, Element) {//Use the jquery Ajax method to verify that the customer name already exists varFlag =false; $.ajax ({type:"POST", URL:' @Url. Action ("Testclientnameisrepeat", "Clientext", new {area = "Client"}) ', Async:false,//synchronous method, if used asynchronously, flag is always 1Data: {' name ': $ ("#DeptName"). Val (), ' ClientId ': $ ("#ClientID"). Val ()}, Success:function(data) {flag=data. Isnamerepeate; } }); return!Flag; }, "The customer name already exists in the customer data and is not added repeatedly." ");});
Background detection method
PublicJsonresult Testclientnameisrepeat (stringNameintClientId =0) { if(string. IsNullOrEmpty (name)) {returnJson (New{isnamerepeate =false}, Jsonrequestbehavior.allowget); } returnJson (New{isnamerepeate= Clientrepository.getclients (p=> p.clientid! = ClientID && (p.name = = Name | | P.deptname = = name),false). Any ()}, Jsonrequestbehavior.allowget); }
Extensions: Error messages for custom methods, such as who added this record to a query, can refer to the following code
$.validator.addmethod (' Pd_password ',function(value, Element) {varLen =value.length; if(len<6) {$ (Element). Data (' error-msg ', ' length cannot be less than 6 bits '); return false; } if(len>15) {$ (Element). Data (' error-msg ', ' length cannot be greater than 15 bits '); return false; } return true;}, function(params, Element) {return$ (Element). Data (' error-msg ');});
Jquery Validator Adding custom validation methods