I'm trying to descentralize my form validators by having generalized functions that may work with any form. In some cases, I use an Ajax Call to make the validation in the server and then send the results back to Javascript. But I can't send the data back to the function as an object. Consider the example:
function isValidEmail(email){ var result = ajaxCallFunction(email);
if (result.valid == true){
return true;
} else {
return false;
}
};
function ajaxCallFunction(email){
$.ajax({
method: "POST",
url: "validateEmail.php",
data: {'email': email},
dataType: 'json',
success: function (response) {
return response;
}
});
}
|||| PHP - validateEmail.php ||||
Performs email validation and existance check in the database and return a json object with the attributes 'valid' (true/false) and 'exists' (true/false);
The problem is with the return I get "Uncaught TypeError: Cannot read property 'valid' of undefined", even though validateEmail.php always send an Object with that attribute. It might be some misunderstanding of javascript objects, but I can't figure out how it realy works.
Some explanation on that would be satiscatory, as well as some advice in the data model I'm using.