allow only numbers in textbox javascript onkeypress
In this article, we will explain to you how to allow only numbers in textbox javascript on keypress. if you want to allow only numeric value in the textbox. you can use the onkeypress or onkeyup event. This is very useful for Telephone numbers, IDs, Postcodes.
You can use the new HTML5 input type ‘number’.But it is dependent on the browser that supports it or not. so sometimes we need to allow the only numbers in textbox using javascript. At the time we need this type of example. so you can see the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> <html lang="en"> <head> <title>Allow Only Numbers In Textbox Javascript On Keypress - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript"> function isNumber(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } </script> </head> <body> <input type="text" id="phone" onkeypress="javascript:return isNumber(event)" placeholder="Enter Phone Number" name="phone"> </body> </html> |
Please follow and like us: