Javascript Code Comparing Two textbox's value

Asked By myzonal.com myzonal.com
27-Oct-09 03:21 AM
Earn up to 0 extra points for answering this tough question.

i ve take two textboxes so i want to compare both textboxes value and print a message that value not same like this with javascript so plz help me soon??????

thanks and regards

vivek

  reply

Deepak Sonawane replied to myzonal.com myzonal.com
27-Oct-09 03:31 AM
Hi,
Your best bet is a custom validator or compare validator, however here you go: if both controls are server controls: <script language="javascript"> function check() {
 var a = document.getElementById('<%=label.ClientID%>');
 var b = document.getElementById('<%=textbox.ClientID%>');  if (a.innerText == b.value) {
  return true;
 } else {
  return false;
 }
} </script>

  Re

Huggy Bear replied to myzonal.com myzonal.com
27-Oct-09 03:31 AM
Below function will do it for you

function StringCompare()
{
var string1 = document.getElementById("TextBox1").value;
var string2 = document.getElementById("TextBox2").value;

if(string1 == string2)
{
alert("Both values are same.");
}
else
       {
alert("Values are different.");
}
return false;
}
//in the button event call the javascript
<asp:Button id="Button1" OnClientClick="StringCompare();" runat="server" Text="Button1"/>

  Javascript Code Comparing Two textbox's value

Sagar P replied to myzonal.com myzonal.com
27-Oct-09 03:31 AM
<html>
<head>
<title>Compare</title>
<script type="text/javascript">
<!--
function compare1()
{
var str1 = document.getElementById("string1").value;
var str2 = document.getElementById("string2").value;
if(str1 != str2)
{
if(str1 > str2)
{
document.getElementById("ans").value= str1 + " Greater than " + str2;
}
else
{
document.getElementById("ans").value= str1 + " Less than " + str2;
}
}
else
{
document.getElementById("ans").value= str1 + " Equals " + str2;
}
}
// -->
</script>
</head>
<body>
 
<h1> Compare</h1>
<hr />
 
<p>Press F5 or Refresh to load script again. This is a program to Compare two input strings.</p>
<form name = "compare" action = "">
<table border = "1">
<caption>Compare</caption>
<tr><td>Enter any String</td>
<td><input name = "string1" type = "text" />
</td></tr>
<tr><td>Enter Second String</td>
<td><input name = "string2" type = "text" />
</td></tr>
<tr><td>Output Comparison</td>
<td><textarea name="ans">
</textarea>
</td></tr>
<tr><td><input type = "button" value = "Compare"
onclick = "compare1()" /></td></tr>
</table>
</form>
</body>
</html>
  Compare Textbox' Values using JS
Vishal Chourasiya replied to myzonal.com myzonal.com
27-Oct-09 04:34 AM

Take 2 textboxes and compare their's values like

function txtValuesCompare()

{

     var txt1 = document.getelementbyid("textbox1").value;

     var txt2 = document.getelementbyid("textbox2").value;

     if(txt1 == txt2)

    {

          alert("values are same");     

          return true;  

    }

    else

    {

          alert("values are not matching"); 

          return false;      

    }

}

<asp:textbox id="textbox1" runat="server"></asp:textbox>

<asp:textbox id="textbox2" runat="server"></asp:textbox>

<asp:button id = "btnCompare" runat = "server" onclientclick = "txtValuesCompare();" text="Compare" /><asp:button>

Create New Account