Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

String Comparison(s)  RSS feed

 
David Lindsey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am new to JS and am stumped:

I have two fields that require a word be entered. The first word should mirror the second and visa versa. If both are equal, then when the user clicks a button, an alert displays stating: "The words are the same".

However, if the word in the second field is different (even if the case is different: ex: bob, Bob) then, after the user clicks the button, an alert displays stating: "The words are not the same".

What I want to do is set up two separate scenarios where:

(1) The uppercase is recognized (code displayed below)
(2) The uppercase is ignored

Any ideas?

Here's my current code:

function compareWords()
{
if (window.document.myLabForm.word1.value ==
window.document.myLabForm.word2.value)
{
window.alert("The words are the same!");
return false;
}
else
{
window.alert("The words are not the same!");
return false;
}
return true;
}


Thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Report post to moderator
There are two methods of String that do what you want:

if (string1.equalsIgnoreCase(string2))
..do.something...

if (string1.equals(string2))
..do.something..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
This is JavaScript, not Java. Followups here, please.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!