Click here to Skip to main content
15,037,420 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
Hi,
I used Two textboxes in my asp.net application. i want to compare that two values on buuton click. How to Check that two textbox values and display alert message using javascript and how to call that function on button click event.


If any one know about this Help me.

Regards
Nanda Kishore.CH
Posted
Comments
Member 11268391 29-Nov-14 8:22am
   
How to call javascript function on text

Try this...its so simple

JavaScript
<script type="text/javascript" language="javascript">
 function ValidateForm()
    {
           var Form=document.getElementById('frmDemo');
           alert(Form.txtbox1.value);
           alert(Form.txtbox2.value);
           if(parseFloat(Form.txtbox1.value)<parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
//or
            if(parseFloat(Form.txtbox1.value)==parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
//or
            if(parseFloat(Form.txtbox1.value)<parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
    }

</script>


Call ValidateForm() on Button OnClientClick
C#
<asp:Button ID="BtnSubmit" Text="Proceed" runat="server" OnClientClick="return ValidateForm();"/>



Thanks
Ashish

Mark it solution if this is your answer
   
v2
Comments
Member 11881367 22-Aug-15 8:47am
   
dfss
In asp button there is a property called OnClientClick there u can specify the java script function like given below

XML
<asp:button id="btnSave" tabIndex="32" runat="server" CssClass="btn" OnClientClick = "return compare()"Text="Save"></asp:button>


in ur javascipt function now u can write the compare function like given below

XML
<script language="javascript" type="text/javascript">


            function compare() {

var txt1 = document.getElementById('txt1').value; // textbox 1 ID is txt1
var txt2 = document.getElementById('txt2').value; // textbox 2 ID is txt2

                if (txt1 == txt2) {
                    alert("both are same");
                }
                else {
                    alert("both are different");
                }

            }

        </script>



Check this out.
   
USe This:

ASP.NET
<asp:button id="Btn_submit" onclientclick="return validate()" onclick="Btn_submit_Click()"/>


Here The OnClientClick is used for calling the javascript function. Here validate is a javascript function.

JavaScript
<script type="text/javascript">
function vaildate()
{
...................
...................
....................
}
</script>
   
v3
You can use this in page_load

Button1.Attributes.Add("onclick", "return javascriptfunction();");

if you don't want postback always return false;
   
Take one button outside of all divs.. Make it visible and give it height=0px .,, so it wont be shown in page.

<asp:Button ID="btnQuote" runat="server" Text="" Height="0px" onclick="btnQuote_Click" />

now, make a javascript fuction and call it from where yu need to call button click event.

<script>
function myFunction() {

var clickButton = document.getElementById("<%= btnQuote.ClientID %>");
clickButton.click();
}
</script>

now, generate the click event of that button..

protected void btnQuote_Click(object sender, EventArgs e)
{

// yor logic//code

}

That all done.. This worked for me.

All the best.
   
v3

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