How to limit HTML input box to only pure numbers

醉折花枝作酒筹
Release: 2021-04-25 09:21:38
forward
3097 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 theonkeyupevent, there is abug, 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 theonchangeevent. After inputting the content, the result will only be obtained wheninputloses focus, and cannot be done while typing. The response

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

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

Code sample

    input 
只能输入纯数字的输入框:
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
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!