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.
<!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: