[jQuery] Validation: AJAX-based without "remote"?
Hi,
I need to do some AJAX-based validation to check that the email
address entered isn't in the database already, but only if the "I am a
new user" radio button has been checked.
I know that the Validation plugin has a "remote" rule, and rules that
can validate one element based on the value of another. But I couldn't
work out how to combine those two, so I tried rolling my own method:
$.validator.addMethod("non-duplicate-email-for-new-customer",
function() {
// If customer is new, check the email address isn't in the database
already
var passValidation = true;
var sEmail = $('#txtEmail').val();
var iCustType = $('input[name="radCustomer"]:checked').val();
if ((sEmail!='') && (iCustType == 1)){
console.log("checking");
$.get("_ajax_emailexists.asp", { email: sEmail }, function(data){
console.log("data:" + data);
passValidation = (data=='true') ? true : false;
console.log("passValidation:" + passValidation);
return passValidation;
});
}
console.log("returning " + passValidation);
}, "This email address is taken.");
This doesn't seem to be working. Describing exactly what it's doing is
a little tricky, and I suspect that if I could, I'd be able to figure
out what was wrong with it. But I think it might be returning before
the AJAX call has completed. In any case, it constantly returns true
and the error message is constantly displayed.
Is this a simple fix or am I going about this the wrong way?