最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Unable to compare two Strings in javascript - Stack Overflow

programmeradmin2浏览0评论

I am trying two pare two strings in JavaScript. But I guess there is some problem while paring. 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.

I am trying two pare two strings in JavaScript. But I guess there is some problem while paring. 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.

Share Improve this question edited Mar 3, 2012 at 5:16 dyoo 12k1 gold badge37 silver badges45 bronze badges asked Mar 3, 2012 at 5:03 adeshadesh 8524 gold badges14 silver badges24 bronze badges 3
  • 2 Did you even check the value of msg? – takteek Commented Mar 3, 2012 at 5:07
  • with out if condition it shows me false on web page. But with if it fails. – adesh 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. – dyoo Commented Mar 3, 2012 at 5:20
Add a ment  | 

2 Answers 2

Reset to default 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

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

if( msg.match(/\s*false\s*/i) )
发布评论

评论列表(0)

  1. 暂无评论