By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

compare strings -- what's the trick?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
> 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.