Tuesday, 14 January 2014

Allow only Numeric Value in textbox in C# ASP.NET using JAVASCRIPT

Some times a user search these types of questions :-

  • Allowing only certain character in a text box.
  • Validation in TextBox which accepts only numbers value.
  • Allow only numbers / digits in TextBox.
  • Prevent user to enter manual text in TextBox.

I am Going to implement a function which will answer all of these questions.
<script type="text/javascript">
    function allowOnlyNumeric()
    {
        if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8)
        {
            return false;
        }
    }
</script> 


If you want to prevent user to enter any other value except numeric, just do like this
 
<asp:TextBox ID="txtNumber" runat="server" onkeypress="return allowOnlyNumeric()"></asp:TextBox>


If you want to prevent user to enter any manual text, just do like this
<asp:TextBox ID="txtNumber" runat="server" onkeypress="return false;"></asp:TextBox>

That’s it!!…..Happy Programming...






No comments:

Post a Comment