I have created a custom validation rule (uniqueUserName). The problem I’m having is that the error message is allways presented for the custom rule, even when returning true from the custom validation method.
The form I am trying to validate is loaded dynamically and added to divs.
Here’s where I add the method and defined the validation rules. The addValMethod is called after the page has been loaded but before attacheValidation:
function addValMeth(){
$.validator.addMethod("uniqueUserName", function(value, element) {
$.get("databasescripts/validate_username.php", {username: value}, function(data2){
alert("Validate-> " + data2);
if(data2 == "1"){
alert("Validate 2");
return true;
}else if(data2 == "0"){
alert("Validate 3");
return false;
}
});
});
};
function attacheValidation($form){
$($form).validate({
rules: {
firstname: "required",// simple rule, converted to {required:true}
surname: "required",
address: "required",
zipcode: "required",
city: "required",
email: {
required: true,
email: true
},
username: {
uniqueUserName: true,
required: true
}
},
messages: {
firstname: "Förnamn saknas" ,
surname: "Efternamn saknas",
address: "Adress saknas",
zipcode: "Postkod saknas",
city: "Ort saknas",
companyname: "Företagsnamn saknas",
email: "Korrekt emailadress saknas",
username: {
uniqueUserName: "Användarnamnet är upptaget",
required: "Användarnamn saknas"
}
}
});
};
The alerts proves the the logic in the custom validator works as it should. All other validation rules work properly.
I don’t know if it could be related, but everytime true result is returned, the custom validation method is requested two times. When it is false, the request to the method is made only once.
It’s the username field I want to apply the custom validation method on.
One has to do return “true” and return “false”. It expects a string not a boolean value.