javascript - Return value from a asyncronous request using a callback function -
this question has answer here:
- how return response asynchronous call? 25 answers
i have httprequest returns value. i'm capturing such value callback function.
the code executes , alert if username duplicate in db. "return false" not working , form submitted (savenewuser) duplicated username anyway. examples i've seen far stop @ callback alert have in code. how accomplish return false stop execution in other cases: first, last name , password checks?
thank 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 blank!"); document.getelementbyid('first_name_id').focus(); return false; } if(document.getelementbyid('last_name_id').value === ""){ alert("last name cannot blank!"); document.getelementbyid('last_name_id').focus(); return false; } checkusername(function(result) { if(result > 0){ alert("error: username exist. please select 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 @ least 1 number (0-9)!"); document.getelementbyid('password_id').focus(); return false; } savenewuser(first_name,last_name,username,password); }
by return false
return callback
function. have return checknewuserform()
function stop saving new user. happen, can use boolean
variable hasduplicateusername
check whether duplicate username entry there, can make true, inside callback. , before savenewuser()
should check whether false
.
function checknewuserform(){ var hasduplicateusername = false; if(document.getelementbyid('first_name_id').value === ""){ alert("first name cannot blank!"); document.getelementbyid('first_name_id').focus(); return false; } if(document.getelementbyid('last_name_id').value === ""){ alert("last name cannot blank!"); document.getelementbyid('last_name_id').focus(); return false; } checkusername(function(result) { if(result > 0){ alert("error: username exist. please select different username!!"); document.getelementbyid('username_id').focus(); hasduplicateusername = true; } }); re = /[0-9]/; if(!re.test(document.getelementbyid('password_id').value)) { alert("error: password must contain @ least 1 number (0-9)!"); document.getelementbyid('password_id').focus(); return false; } if(!hasduplicateusername) { savenewuser(first_name,last_name,username,password); } }
Comments
Post a Comment