0

I am trying two compare two strings in JavaScript. But I guess there is some problem while comparing. It doesn't show the results.

if(ajaxRequest.readyState == 4){

    var msg = ajaxRequest.responseText;
    var fld = document.getElementById("prtCnt");
    if(msg == "false") {
        var msg = "This User Name is already taken !!!!";
        fld.className = "bp_invalid";
        //   fld.style.color=green;
        fld.innerHTML=msg;
    }

Can any body tell me where the problem is? Thanks.

3
  • 2
    Did you even check the value of msg? Commented Mar 3, 2012 at 5:07
  • with out if condition it shows me false on web page. But with if it fails. Commented Mar 3, 2012 at 5:09
  • 2
    You might as well add the following "print" statement to see what value you are receiving in msg: alert(msg); Put that before your if(msg == 'false')... test, and amend your question with that additional information. Commented Mar 3, 2012 at 5:20

2 Answers 2

5

You might want to check if there's any space before or after the "false" string that you return from the server. You can do it easily with this:

alert('"' + msg + '"');

If there is extra space, you can just do:

msg = msg.trim();

and then do your if statement

Sign up to request clarification or add additional context in comments.

4 Comments

it worked thanks a lot. But can you tell me why there was whit space in msg While i was just returning a false word via out.print("false");
Are you calling out.print("false"); from a jsp page? Maybe there's extra space before the starting tag or after the closing tag?
How can i get rid of Whit spaces . Is there any way instead of using trim() method as IE doesn't support it. Thanks
Pffious lost half a hour to discover my string had a whitespace somewhere.
1

Ensure that there is no white space around the word "false" with something like:

if( msg.match(/\s*false\s*/i) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.