How to get the return value true and false.
I always get undefined and after read some article i still didn't understand.
My reference:How to return value from an asynchronous callback function?
function validateScreenName(value){
var screenname = document.getElementById("screenname-box").value;
if((screenname.length < 3) || (screenname.length > 20)){
value(false); // if I change to return false still can't get the value. Always undefined.
}else{
if(screenname != "something"){
var http = new XMLHttpRequest();
var param = "screen_name="+screenname;
var url = "screen-name-validation.php";
http.open("POST",url,true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function(){
//The response from server is either 1 or 0
if (http.readyState==4 && http.status==200){
if(http.responseText == 0){
value(true); // I can't get this value
}else{
value(false);
}
}
}
http.send(param);
}else{
value(false);
}
}
}
I need this value to this script
function disableSubmit(){
validateScreenName(function(val2){
var val1 = validateFullName();
var val3 = validateTargetName();
//I need val2 from validateScreenName
if(val1 && val2 && val3){
//if all true do something
}else{
//if one of the value false do somthing
}
});
}
Can someone explain how to get the value with only javascript without jquery? thanks.