Connecting Tech Pros Worldwide Forums | Help | Site Map

compare strings -- what's the trick?

J.W.
Guest
 
Posts: n/a
#1: Jul 20 '05
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.




Laurent Bugnion, GalaSoft
Guest
 
Posts: n/a
#2: Jul 20 '05

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

Douglas Crockford
Guest
 
Posts: n/a
#3: Jul 20 '05

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(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;
>
> }[/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(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

Closed Thread


Similar JavaScript / Ajax / DHTML bytes