how to enter only numbers in Textbox, asp .net

Posted on Posted in .NET

Recently I was struck with a problem of allowing only numbers in Textbox  , I searched many sites on google, finally found the solution to the problem. the answer is

Write down following javascript function on your page (.aspx)

<script type="text/javascript">   
function fncInputNumericValuesOnly()      {         if (!(event.keyCode == 45 || event.keyCode == 46 || event.keyCode == 48 || event.keyCode == 49 ||         event.keyCode == 50 || event.keyCode == 51 || event.keyCode == 52 || event.keyCode == 53 ||         event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56 || event.keyCode == 57))          {         event.returnValue = false;         }     }
       </script>


And call this function on KeyPress Event of your TextBox

<asp:TextBox id="TextBox1"  runat="server" onkeypress= "fncInputNumericValuesOnly(TextBox1)"/>

if you come across any problem, feel free to comment here.

Source: Jem Savery:  nullskull.com

Leave a Reply

Your email address will not be published. Required fields are marked *