Home > Web Front-end > JS Tutorial > body text

How to set javascript to only enter numbers

coldplay.xixi
Release: 2023-01-04 09:35:14
Original
10104 people have browsed it

Javascript method to set only numbers: first use the input tag to create a text input box, and set the button to execute the [pan()] function; then use the [getElementById()] method to obtain the input box through the value attribute content; finally click the button to view the judgment results.

How to set javascript to only enter numbers

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

How to set only numbers in javascript:

1. Create a new html file and name it test.html. It is used to explain js to judge the content of the input box. Only Numbers are allowed.

How to set javascript to only enter numbers

2. In the test.html file, use the input tag to create a text input box and set its id attribute to myinput.

How to set javascript to only enter numbers

3. In the test.html file, use the button tag to create a button, bind the onclick() click event to the button, and when the button is clicked, execute pan( )function.

How to set javascript to only enter numbers

4. In the test.html file, in the js tag, create a pan() function. Within the function, use the getElementById() method to obtain input through the value attribute. The contents of the box.

How to set javascript to only enter numbers

#5. Within the pan() function, create a regular expression that matches numbers and save it in the re variable.

How to set javascript to only enter numbers

6. In the test.html file, use the if statement and use the test() method to use regular expressions to determine whether the content of the input box is a number. If it is not a number, Use the alert() method to prompt "Please enter a number."

How to set javascript to only enter numbers

7. Open the test.html file in the browser, enter non-numeric content in the input box, and click the button to view the judgment results.

How to set javascript to only enter numbers

Related free learning recommendations: javascript learning tutorial

The above is the detailed content of How to set javascript to only enter numbers. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!