How to set the text box so that numbers cannot be entered in javascript

WBOY
Release: 2022-01-17 17:46:18
Original
2653 people have browsed it

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

How to set the text box so that numbers cannot be entered in javascript

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:

How to set the text box so that numbers cannot be entered in javascript

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(&#39;test&#39;).addEventListener(&#39;keypress&#39;,function(e){
             var charCode=e.charCode;
             if(charCode>46&&charCode<58)  /*0-9 的charcode*/
             e.preventDefault();
     });
    }
    </script>
</body>
</html>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template