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

How to limit HTML input box to only pure numbers

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-04-25 09:19:543200browse

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,'')"

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,'')"

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,'')"

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>

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete