46&&charCode <58)"."/> 46&&charCode <58)".">
Home > Article > Web Front-end > How to set the text box so that numbers cannot be entered in javascript
In JavaScript, you can use if statements and keycode to set the text box so that numbers cannot be entered. The keycode value from 0 to 9 is 48 to 57. As long as the keycode value in the control text box is not within this range, the syntax is: "if(charCode>46&&charCode
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
How to set the text box in JavaScript so that numbers cannot be entered
The value of the keycode for numbers 0-9 is 48-57, as long as the text box is set in JavaScript If the value of keycode entered in this range is within this range, this behavior will be cancelled, so that the text box cannot enter numbers.
Examples are as follows:
Or:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <input type="text" id="test"> <script type="text/javascript"> window.onload=function(){ document.getElementById('test').addEventListener('keypress',function(e){ var charCode=e.charCode; if(charCode>46&&charCode<58) /*0-9 的charcode*/ e.preventDefault(); }); } </script> </body> </html>
The output result is the same as the above, and numbers cannot be entered in the text box.
【Related recommendations: javascript learning tutorial】
The above is the detailed content of How to set the text box so that numbers cannot be entered in javascript. For more information, please follow other related articles on the PHP Chinese website!