Hi,
J.W. wrote:
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(val,val2, str) {
var strInput = new String(val.value);
var strInput2 = new String(val2.value);
if (strInput!=strInput2) {
alert(str);
val2.focus();
val2.select();
return false;
} else
return true;
}
-----
Thanks for your help.
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