Reputation: 155
I am using following code to show custom messages inside add method of jquery validation plugin. I referred this link: http://www.mainframes.co.uk/index.php/2011/04/07/jquery-form-validator-custom-validation-method-using-addmethod-for-validating-date-of-birth-dob/
$.validator.addMethod("nameId",function(value,element){
var result = true;
//check if pattern matches
var pattern = new RegExp(<pattern>);
if(pattern.test(value)){
//get availability via ajax call
$.ajaxSetup({
async: false,
"error":function() {
alert("error");
}});
$.getJSON("<url>",
{
param: value
},
function(data) {
if(condition){
result = true;
}else{
$.validator.messages.nameId = "Msg 1";
result = false;
}
});
}else{
$.validator.messages.nameId = "Msg 2";
result = false;
}
return result;
},"");
This doesn't show any errors while validating. What could be wrong above...
Upvotes: 0
Views: 3041
Reputation: 125
Add this just before your return result line:
$.validator.messages.myvalidator = customError;
Upvotes: 1
Reputation: 2459
What happens if you put the .getJSON call inside of the success callback function of the .ajaxSetup? Like this:
$.validator.addMethod("nameId",function(value,element){
var result = true;
var pattern = new RegExp(<pattern>);
if(pattern.test(value)){
//get availability via ajax call
$.ajaxSetup({
async: false,
"success": function() {
$.getJSON("<url>",
{
param: value
},
function(data) {
if(condition){
result = true;
}else{
$.validator.messages.nameId = "Msg 1";
result = false;
}
});
},
"error":function() {
alert("error");
}
});
}else{
$.validator.messages.nameId = "Msg 2";
result = false;
}
return result;
},"");
Upvotes: 2
Reputation: 15390
You cant have asynchronous callbacks inside a validation method.
Upvotes: -1