Home > Web Front-end > HTML Tutorial > How to limit HTML input box to only pure numbers

How to limit HTML input box to only pure numbers

醉折花枝作酒筹
Release: 2021-04-25 09:21:38
forward
3207 people have browsed it

This article will introduce to you how to limit the input input box to only pure numbers in HTML. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

onkeyup = "value=value.replace(/[^\d]/g,'')"
Copy after login

Using the onkeyup event, there is a bug, that is, in the Chinese input method state, enter directly after inputting Chinese characters , the letters

onchange = "value=value.replace(/[^\d]/g,'')"
Copy after login

will be input directly using the onchange event. After inputting the content, the result will only be obtained when input loses focus, and cannot be done while typing. The response

oninput = "value=value.replace(/[^\d]/g,'')"
Copy after login

uses the oninput event, which perfectly solves the above two problems. There are no other problems in the test yet.

Code sample

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>input</title>
    </head>
    
    <body>
    只能输入纯数字的输入框:<input type="text" name="" oninput="value=value.replace(/[^\d]/g,&#39;&#39;)">
    </body>
    </html>
Copy after login

Recommended learning: html video tutorial

The above is the detailed content of How to limit HTML input box to only pure numbers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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