How to limit the input box to only input pure numbers in HTML

How to limit the input box to only input pure numbers in HTML

Limit input box to only pure numbers

1、onkeyup = "value=value.replace(/[^\d]/g,'')"

There is bug when using the onkeyup event. In the Chinese input method, if you enter a Chinese character and then press Enter, the letter will be entered directly.

2、onchange = "value=value.replace(/[^\d]/g,'')"

When using onchange event, after entering content, the result will only be obtained when input loses focus, and it will not respond when entering.

3、oninput = "value=value.replace(/[^\d]/g,'')"

Using oninput event perfectly solves the above two problems, and no other problems have occurred in the test so far.

Code Sample

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>input</title>
</head>
<body>
    An input box that can only input pure numbers: <input type="text" name="" oninput="value=value.replace(/[^\d]/g,'')">
</body>
</html>

This is the end of this article about how to limit the input box in HTML to only input pure numbers. For more relevant HTML content about limiting input to numbers, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of the role of overflow:hidden (overflow hiding, clearing floats, solving margin collapse)

>>:  An example of implementing a simple finger click animation with CSS3 Animation

Recommend

Detailed explanation of how to use several timers in CocosCreator

1. setTimeOut Print abc after 3 seconds. Execute ...

Centos7.5 configuration java environment installation tomcat explanation

Tomcat is a web server software based on Java lan...

Talk about the understanding of CSS attribute margin

1.What is margin? Margin is used to control the sp...

How to delete garbled or special character files in Linux

Due to encoding reasons, garbled characters will ...

Implementing file content deduplication and intersection and difference in Linux

1. Data Deduplication In daily work, there may be...

MySQL slow log online problems and optimization solutions

MySQL slow log is a type of information that MySQ...

Example of Html shielding right-click menu and left-click typing function

Disable right-click menu <body oncontextmenu=s...

MySQL 5.7.17 installation and configuration tutorial under CentOS6.9

CentOS6.9 installs Mysql5.7 for your reference, t...

How to install MySQL 8.0 and log in to MySQL on MacOS

Follow the official tutorial, download the instal...

How to deploy redis in linux environment and install it in docker

Installation Steps 1. Install Redis Download the ...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...