Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In My webpage I hav A Textbox Field,
In which I want to allow Only numeric Charecters,;How can i do it in c#
Posted
Updated 30-Jan-12 0:46am
v3

 
Share this answer
 
please write javascript for allowing numeric only

C#
<pre>function AllowNumber(evt)// These function is used for Inserting only number
         {
            var charCode;
            charCode = (evt.which) ? evt.which : event.keyCode;
            if(charCode >=48 && charCode<=57)
            {
            return true;
            }
            else
            {
                alert("Please Enter Number Only");
                return false;
            }
         }



and call to your code like that

<pre><asp:textbox id="txtAMCAmount" runat="server" cssclass="TextBoxStyle" width="185px" onkeypress="return AllowNumber(this)" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 
use this javascript

C#
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;

}
 
Share this answer
 
Please check below code

XML
<head  runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function CheckNumeric(e) {

            if (window.event) // IE
            {
                if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
                    event.returnValue = false;
                    return false;

                }
            }
            else { // Fire Fox
                if ((e.which < 48 || e.which > 57) & e.which != 8) {
                    e.preventDefault();
                    return false;

                }
            }
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:textbox id="TextBox1" runat="server" onkeypress="CheckNumeric(event);"
             xmlns:asp="#unknown"></asp:textbox>
    </form>
</body></head>
 
Share this answer
 
v2
 
Share this answer
 
You can do this with ajax also.

C#
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc" %>
<asp:TextBox ID="TextBox1" runat="server" AutoComplete="off"></asp:TextBox>

 <cc:FilteredTextBoxExtender ID="FilteredTextBoxExtender1"  runat="server"
    TargetControlID="TextBox1"         
   FilterType="Numbers" 
    />
 
Share this answer
 
v3
C#
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (char.IsControl(e.KeyChar)==false)
               if (char.IsDigit(e.KeyChar) == false)
               {
                   e.Handled = true;
                   TextBox1.Focus();
               }
       }
 
Share this answer
 
v2
C#
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    string str = txtAge.Text;                               //  User Control
    int len = str.Length;
    int index=0;
    int dcml = 2;                                           // 2 indeicates the Deccimal Points Needed   (User Defined)
    if (str.Contains('.'))
    {
        index = str.IndexOf('.');
        index = len - index;
    }
    if (Char.IsControl(e.KeyChar) == false)
    {
        if (((Char.IsDigit(e.KeyChar)) && (index <= dcml)) || ((e.KeyChar == '.') && (!(str.Contains('.')))))
        {
        }
        else
        {
            e.Handled = true;
        }
    }
}




This Works Better.. It Accepts only Numbers and decimal Points upto 2 Decimal Places.. If you need to change the Decimal place.. you can edit in the code in 'dcml'.. change the "int dcml = 2" to whatever you like.
 
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