20

I want compare two variables, that are strings, but I am getting an error.

<script>
    var to_check=$(this).val();
    var cur_string=$("#0").text();
    var to_chk = "that";
    var cur_str= "that";

    if(to_chk==cur_str){
        alert("both are equal");
        $("#0").attr("class","correct");    
    } else {
        alert("both are not equal");
        $("#0").attr("class","incorrect");
    }
</script>

Is something wrong with my if statement?

10
  • This should do the trick, its not where your problem is. Show the code. Commented Apr 1, 2013 at 5:16
  • please post more code with how you get to_chk and cur_str ... can't tell from that Commented Apr 1, 2013 at 5:16
  • 3
    You haven't mentioned what error you are getting.. Commented Apr 1, 2013 at 5:30
  • This code works in Chrome and FF. Commented Apr 1, 2013 at 5:41
  • try to delete any whitespace from begining and end of string and convert to lower case Commented Apr 1, 2013 at 6:53

5 Answers 5

20

=== is not necessary. You know both values are strings so you dont need to compare types.

function do_check()
{
  var str1 = $("#textbox1").val();
  var str2 = $("#textbox2").val();

  if (str1 == str2)
  {
    $(":text").removeClass("incorrect");
    alert("equal");
  }
  else
  {
    $(":text").addClass("incorrect");
    alert("not equal");
  }
}
.incorrect
{
  background: #ff8888;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="textbox1" type="text">
<input id="textbox2" type="text">

<button onclick="do_check()">check</button>

Sign up to request clarification or add additional context in comments.

1 Comment

I had same issue today and comparison would not work. After spending couple of hours pulling my hair, i figured i was initializing my variable as var x; and comparison did not work. When I changed var x=""; it worked. So I am guessing, it was using object comparison as oppose to string comparison.
6

instead of using the == sign, more safer use the === sign when compare, the code that you post is work well

2 Comments

No, both values in the comparision will be strings.
how do you achieve this? if(a b) a = 12345 , b = 123 to be true, because a has all of b elements?
3

You can use javascript dedicate string compare method string1.localeCompare(string2). it will five you -1 if the string not equals, 0 for strings equal and 1 if string1 is sorted after string2.

<script>
    var to_check=$(this).val();
    var cur_string=$("#0").text();
    var to_chk = "that";
    var cur_str= "that";
    if(to_chk.localeCompare(cur_str) == 0){
        alert("both are equal");
        $("#0").attr("class","correct");    
    } else {
        alert("both are not equal");
        $("#0").attr("class","incorrect");
    }
</script>

Comments

2

Try this , it works.

<script>
var to_check=$(this).val();
var cur_string=$("#0").text();
var to_chk = "that";
var cur_str= "that";
if(to_chk.localeCompare(cur_str) === 0){
    alert("both are equal");
    $("#0").attr("class","correct");    
} else {
    alert("both are not equal");
    $("#0").attr("class","incorrect");
}

1 Comment

Copied 1:1 from Kiran Chenna's answer.
1

I used below function to compare two strings and It is working good.

function CompareUserId (first, second)
{

   var regex = new RegExp('^' + first+ '$', 'i');
   if (regex.test(second)) 
   {
        return true;
   }
   else 
   {
        return false;
   }
   return false;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.