Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to compare two strings. if their cases are dissimilar than also it alerts a error. for this my code is as follows:
JavaScript
var a,b;

a = document.f1.t1.value;
b = document.f1.t2.value;
bb = '';
cc = '';

for(i=0;i<a.length;i++)
{
    for(j=0;j<b.length;j++)
    {
        if(b.charAt(j) >=65 && b.charAt(j) <= 90)
        {
            bb = bb + b.charAt(j);
        }
        else
            bb = bb +b.charAt(j);
    }

    if (a.charAt(i) > =65 && a.charAt(i) <= 90)
    {
        cc = cc + a.charAt(i);
    }
    else
        cc = cc + a.charAt(i);
    }

    if (bb == cc)
    {
        alert("strings are equal");
    }
    else
        alert("strings are unequal");
</script>
</head>
<body>
    <form name = "f1">
        <input type ="text" name ="t1">
        <input type ="text" name ="t2">
        <input type="button" value="ok" onclick="rev()">
    </form>
</body>
</html>

but is is not executing.where i am wrong?
Posted
v4

JavaScript
<script type="text/javascript">
    var username = "Agent006";
    if(username == "Agent007")
    	document.write("Welcome special agent 007"); 
    else
    	document.write("Access Denied!"); 
    document.write("<br /><br />Would you like to try again?<br /><br />");
    
    // User enters a different name
    username = "Agent007";
    if(username == "Agent007")
    	document.write("Welcome special agent 007"); 
    else
    	document.write("Access Denied!"); 
</script>


Hope this helps.
 
Share this answer
 
v2
Hi,
For Comparing two strings :
JavaScript
function validate()
{
       a = document.f1.t1.value;
       b = document.f1.t2.value;
if(a.Length==b.Length)
{
       for(i=0;i<a.length;i++)>
        {
               if(a.charcodeat(i)!=b.charcodeat(i))
               alert('Something went wrong ');
               return false;
                
        }
}
else
{
  alert('Something went wrong');
  return false;
}

}
 
Share this answer
 
Your code makes no sense.
Quote:
if(b.charAt(j) >=65 && b.charAt(j) <= 90)
{
bb = bb + b.charAt(j);
}
else
bb = bb +b.charAt(j);
Here you end up doing the same thing (what?!) either if the condition is true and if it is false.


Quote:
if (bb == cc)
{
alert("strings are equal");
}
Here you are stating the strings are equal based on a single character equality.
 
Share this answer
 
v2
use indexOf() method

ex:
var a = document.f1.t1.value;
var b = document.f1.t2.value;

if ( b.indexOf(a) > -1)
{
alert("data are same");
}
else
{
alert("Data are not same");
}


=indexOf() method will return -1 if value is not occuring.
 
Share this answer
 
What's the problem then?

Run this script

JavaScript
<script language="javascript">
         
var b = "this is a sample string";
var a = "This Is A Sample String";
alert(a == b);
        
</script>


output will be false!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900