I have a HTTPRequest that returns a value. I'm capturing such value with a callback function.
The code executes and alert if the username is duplicate in the DB. However the "return false" is not working and the form is submitted (saveNewUser) with the duplicated username anyway. All the examples I've seen so far just stop at the callback with an alert just like I have in my code. So how do I accomplish that the return false stop the execution like in the other cases: first, last name and password checks?
Thank you so much.
function checkUsername(callbackUsername){
var username = document.getElementById('username_id').value;
var ajaxRequest = getXMLHttp();
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
var response = trim(ajaxRequest.responseText);
callbackUsername(response);
}
};
var url = "../admin/check_unique_username.php";
var parameters = "username="+username;
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(parameters);
}
function checkNewUserForm(){
if(document.getElementById('first_name_id').value === ""){
alert("First Name cannot be blank!");
document.getElementById('first_name_id').focus();
return false;
}
if(document.getElementById('last_name_id').value === ""){
alert("Last Name cannot be blank!");
document.getElementById('last_name_id').focus();
return false;
}
checkUsername(function(result) {
if(result > 0){
alert("ERROR: Username already exist. Please select a different username!!");
document.getElementById('username_id').focus();
return false;
}
});
re = /[0-9]/;
if(!re.test(document.getElementById('password_id').value)) {
alert("Error: password must contain at least one number (0-9)!");
document.getElementById('password_id').focus();
return false;
}
saveNewUser(first_name,last_name,username,password);
}