Click here to Skip to main content
15,896,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
EveryBody

I have a small problem about asp.net. I have a small asp.net form which contains one textbox and one button to send textbox text to server. but i want to send only numeric texts. so i want to evaluate text with javascript and if it wasn't numerical don't send to server but if it was numeric send to server and insert into sqlserver. but each time it sends it to server.

my asp.net code

C#
<pre lang="xml"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" language="javascript">
        function isNumeric(num) {
            return !isNaN(num)
        }

        function validateForm() {
            var x = document.forms["ctl01"]["NumberTxt"].value;
            if (x == null || x == "") {
                alert("First name must be filled out");
                return false;
            }
            if (!isNumeric(x)) {
                alert("it is not numerical value");
                return false;
            }
        }

    </script>
</head>
<body>
   <form runat="server">
    <asp:TextBox ID="NumberTxt" runat="server">hello</asp:TextBox>
    <asp:Button ID="Submit" runat="server" Text="Submit"
        OnClientClick="validateForm()" onclick="Submit_Click" />
    </form>
</body>
</html>

and my server side code for button


protected void Submit_Click(object sender, EventArgs e)
    {
        Response.Write("hello serer");
    }
Posted

1 solution

Try calling for the return value of your JavaScript functions.

like:
ASP.NET
<asp:Button ID="Submit" runat="server" Text="Submit" onClientClick="return validateForm();" onclick="Submit_Click" />


On the button click, if your JavaScript function returns true, then asp.net will perform a postback, if it returns false, no postback will occur.

Hope it helps.
 
Share this answer
 
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