0

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.

Community
  • 1
  • 1
aLIEz
  • 1,206
  • 1
  • 15
  • 17
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Quentin May 06 '14 at 10:02
  • What is so hard to understand about "This is impossible"? – Quentin May 06 '14 at 10:02

1 Answers1

-1

Instead of returning typeof bool(false,true), use returning string..eg.,"true" or "false"

Satz
  • 105
  • 9