I tried using a when / done statement as follows:
$.when(emailValUReq($(this),noRet)).done(function(r){vErr+=r;});
but the problem with this was that 'r' was undefined (I was hoping it would pull the returned value from the emailValUReq function).
For me, the solution was to rework the emailValUReq function to simply update the error variable directly, as follows:
function emailValUReq(v, noRet) {
var textReg = /[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?/;
if (!v.val()) {
if (noRet == null) {
v.nextAll('.errText').html('<br>! Required Field');
}
$err++;
} else if (!textReg.test(v.val())) {
if (noRet == null) {
v.nextAll('.errText').html('<br>! Invalid Entry');
}
$err++;
} else {
$data = new Object;
$data['email'] = v.val();
$data['id'] = v.data('cur');
$.ajax({
type: "POST",
url: "/modules/data/dup_check.php",
data: $data,
success: function (r) {
if (r == 'ok') {
v.nextAll('.errText').empty();
} else {
if (noRet == null) {
v.nextAll('.errText').html('<br>! An account already exists for this email');
}
$err++;
}
}
});
}
}
$err=0;
area.find('.emailvalureq:visible').each(function () {
emailValUReq($(this), noRet);
});
All that I need to then do is delay checking the value of $err for roughly 1 second, which gives the ajax call enough time to run and return the response.
setTimeout(function(){return $err;},1128);
I'm not sure if this was the best response, or the most elegant, but it worked!