Using AJAX during form validation
I have a few cases where I would like to check an email or customer number before submitting a form and do it inline while validating the other form fields. The problem I run into is that it seems the AJAX request returns true or doesn't complete before submitting the form. I have tried this various ways like putting the AJAX request into its own function and having it return true or false to the validation function but that does not seem to work either...Here is a code example...
- $("#companyform").submit(function() {
$.ajaxSetup({ cache: false });
$.post("ajax_v2Functions.cfm", { coNum: $("#companyNumber").val() },
function(response) {
if(response != "") {
alert("That company number is already in use by " + response);
$("#companyNumber").focus();
return false;
}
return false;
} , "html");
if($("#companyname").val() == "") {
alert("The company name cannot be left blank.");
$("#companyname").focus();
return false;
}
if($("#companyNumber").val() == "") {
alert("The company number cannot be left blank.");
$("#companyNumber").focus();
return false;
}
});