Restrict the Textbox to enter only Numbers Using Javascript

Normally we get this type of requirement to accept only numbers in a text box.Suppose we have a Credit Card Number entry where we want to validate the key-press is number or not.Here i have simplified the way to enter only numbers in the text-box.

Here i have written some logic to enter only numbers in textbox using javascript.
<script type="text/javascript">
         function isNumberKey(evt) {
             var charCode = (evt.which) ? evt.which : event.keyCode
             if (charCode > 31 && (charCode < 48 || charCode > 57))
                 return false;
             return true;
         }      
    <script>

Now we can call this function in the events like keypress,keydown and keyup to validate the value is number or not.

Happy Coding :-)

Popular posts from this blog

Exporting to excel from a custom class object using C#.NET

Get the Clicked Cell Value and Column Name of GridView in ASP.NET

How to hide bootStrap popover when user click(s) outside the popover?