I'm having a form with a SELECT field (college_id), which I am trying to validate. I am using the JQuery Validate Plugin and trying to use it's "remote" option.
HTML:
<form id="registration-form" ... >
.
.
<select name="college_id" id="college_id" class="required" >
//Options generated using PHP code
</select>
.
.
</form>
JAVASCRIPT:
$(document).ready(function(){
//For triggering the validation onChange
$('#college_id').change(function(){
$("#college_id").removeData("previousValue");
$("#registration-form").validate().element("#college_id");
});
$('#registration-form').validate({
rules:{
college_id:{
remote: {
type: 'POST',
url: urls.base_url+'Representative/checkCollegeAJAX/',
dataType:"json",
data:{
college_id:function(){
return $('#college_id').val();
}
},
dataFilter: function(data) {
data=JSON.parse(data);
console.log(data.isError);
if(!data.isError) { //If no error
return true;
} else {
console.log("Error Message: "+data.errorMessage);
return "\"" + data.errorMessage + "\"";
}
}
}
}
}
});
});
As you can see above, i'm trying to display the Error message when isError==True
My PHP code returns a JSON encoded object like:
{"isError":true,"errorMessage":"Sorry!"}
In case of an error OR
{"isError":false,"errorMessage":""}
In case of No Error. (Extracted right from Fire Bug)
The problem is that no matter what the response, I keep getting "Fix this field" next to the SELECT field. Only in case there is an error, i need to get my custom message (data.errorMessage) displayed.