469,282 Members | 1,708 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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(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.

Jul 20 '05 #1
2 76216
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

Jul 20 '05 #2
> How do I compare strings in javascript? The "==" double equals or
"!=" doesn't seem to work in this case. -----
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;

}


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(va1, va2, str) {
if (va1.value != va2.value) {
alert(str);
va2.focus();
va2.select();
return false;
} else {
return true;
}
}

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Drew | last post: by
19 posts views Thread by David zhu | last post: by
11 posts views Thread by balakrishnan.dinesh | last post: by
3 posts views Thread by Twinkle | last post: by
50 posts views Thread by titan nyquist | last post: by
2 posts views Thread by Peter Proost | last post: by
3 posts views Thread by mcolson | last post: by
11 posts views Thread by Tony | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.