this is a part of the check in my form
function check(theform) {
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.username.value)) {
alert("not valid username");
theform.username.focus();
return false;
}
$.ajax({
type: "POST",
url: "username.asp",
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
});
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.password.value)) {
alert("not valid password");
theform.password.focus();
return false;
}
}
for some reason of sync… it check the username then duplicated username with the ajax and not waiting for respond and jump to the password check.
i don’t want to insert the rest of the code to isreadystate (or what ever it is) because i might move the username duplicate check to the end… and then the function will end before the ajax anyway
what should i do?
The first A in AJAX stands for “Asynchonous”. The call is made, and the execution of the function continued without waiting for the call to return.
You could set the
asyncoption in your call tofalse, making your call sychronous. However, you would have to change your functions so thereturn falsemakes it through to thecheckfunction, and it is not recommended by the jQuery manual:a better way around it would be to make the checks you can make in “real time” within the function, then start the Ajax request, and submit the form in the success callback of that request.
2nd attempt at a syncrhonous call. This seems to actually work – tested in FF 3.6.