compare strings -- what's the trick?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J.W.

    #1

    compare strings -- what's the trick?

    How do I compare strings in javascript? The "==" double equals or
    "!=" doesn't seem to work in this case.

    I'm sure string comparison has been explained before but searching
    Google didn't find any code that looked similar to this one:

    -----
    function CheckPassword(v al,val2, str) {

    var strInput = new String(val.valu e);
    var strInput2 = new String(val2.val ue);

    if (strInput!=strI nput2) {
    alert(str);
    val2.focus();
    val2.select();
    return false;
    } else
    return true;

    }
    -----


    Thanks for your help.

  • Laurent Bugnion, GalaSoft

    #2
    Re: compare strings -- what's the trick?

    Hi,

    J.W. wrote:[color=blue]
    > How do I compare strings in javascript? The "==" double equals or
    > "!=" doesn't seem to work in this case.
    >
    > I'm sure string comparison has been explained before but searching
    > Google didn't find any code that looked similar to this one:
    >
    > -----
    > function CheckPassword(v al,val2, str) {
    >
    > var strInput = new String(val.valu e);
    > var strInput2 = new String(val2.val ue);
    >
    > if (strInput!=strI nput2) {
    > alert(str);
    > val2.focus();
    > val2.select();
    > return false;
    > } else
    > return true;
    >
    > }
    > -----
    >
    >
    > Thanks for your help.[/color]

    You create two String objects, two different instances which, even if
    they carry the same value, are not the same instance. The equality
    doesn't work.

    A String object is not the same as a string (primitive value). The value
    property of your form element is a primitive string. If you compare two
    primitive string, the equality operator returns true if the content is
    the same. When you compare two objects, the equality operator returns
    true only if it is the same instance.

    To compare your strings, use:

    function CheckPassword( val, val2, str )
    {
    var strInput = val.value;
    var strInput2 = val2.value;

    if ( strInput!= strInput2 )
    {
    alert(str);
    val2.focus();
    val2.select();
    return false;
    }
    else
    {
    return true;
    }
    }

    HTH,

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Douglas Crockford

      #3
      Re: compare strings -- what's the trick?

      > How do I compare strings in javascript? The "==" double equals or[color=blue]
      > "!=" doesn't seem to work in this case.[/color]
      [color=blue]
      > -----
      > function CheckPassword(v al,val2, str) {
      >
      > var strInput = new String(val.valu e);
      > var strInput2 = new String(val2.val ue);
      >
      > if (strInput!=strI nput2) {
      > alert(str);
      > val2.focus();
      > val2.select();
      > return false;
      > } else
      > return true;
      >
      > }[/color]

      Avoid the String conductor. You don't need it. This isn't Java. The String
      constructor doesn't return a string. It returns an object. Yeah. Mistakes were
      made.

      function checkPassord(va 1, va2, str) {
      if (va1.value != va2.value) {
      alert(str);
      va2.focus();
      va2.select();
      return false;
      } else {
      return true;
      }
      }



      Comment

      Working...