473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 76507
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Drew | last post by:
Hello - I am a converted VB programmer. What I am trying to do it compare two strings in an if statement. The problem is that when I use string.compare it always returns a negative 1. I have...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
3
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
3
by: mcolson | last post by:
I am trying to compare the last two values in the same column of a table. First of all, I have a column titled Row_Index that uses an index which starts at 1 and increments by 1. What I am trying...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.