I have done AJAX validation and validated message is returned as a JSON array. Therefore I need to check whether the keys, like name and email, are in that JSON array.
{
"name": {
"isEmpty": "Value is required and can't be empty"
},
"email": {
"isEmpty": "Value is required and can't be empty"
}
}
Only if the key name is present, I need to write an error message to the name field.
Following is the code to display an error if fields is entered
if (obj['name']'isEmpty'] != "") {
$('#name').after(c1 + "<label class='error'>" + obj['name']['isEmpty'] + "</label>");
}
if (obj['email']['isEmpty'] != "" ) {
$('#email').after(c4 + "<label class='error'>" + obj['email']['isEmpty'] + "</label>");
}
But if the name field is entered, it will not be in JSON array.
So the checking statement
if (obj['name']['isEmpty'] != "")
will result in the following error:
obj.name not found
It is not necessary to have key name in the array. At same time I need to check for this to display the error if the array possesses the key name.