Click here to Skip to main content
14,932,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was created consultant registration page using ASP.Net], When we enter any characters in text box that will not accept.. Text Box which accepts only numbers!(numeric values only)!!

<script language="JavaScript">
function onlyNumbers(evt)
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;

<input type="text" onkeypress="return onlyNumbers();">
Add the following javascript to the HEAD area of your page:
    <SCRIPT language=Javascript>
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;

            return true;
Then, add onkeypress to your textbox definition:
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return isNumberKey(event)"></asp:TextBox>
Apply this
<asp:TextBox ID="txtNo" runat="server" Style="width: 245px;"></asp:TextBox>
<asp:RegularExpressionValidator Display="Dynamic" ID="RegNo" runat="server" ControlToValidate=" txtNo " ErrorMessage="Please enter a valid number"ValidationExpression="^[0-9 \+\-]*$" ValidationGroup="ValidAll" ForeColor="#DB3100"></asp:RegularExpressionValidator>
Hi murali,
Go through the following code and you can use the numeric textbox any of your projects.
Creating Numeric TextBox[^]

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