How to restrict HTML input box to only input pure numbers

醉折花枝作酒筹
Release: 2023-01-04 09:34:55
Original
8487 people have browsed it

Method to limit the input input box to only pure numbers: through the oninput event, use the replace() function in the event with the regular expression "/[^\d]/g,''" to limit the input. Achieve the effect that only numbers can be entered in the input text box, and other characters will be automatically cleared when entering other characters.

How to restrict HTML input box to only input pure numbers

The operating environment of this tutorial: windows7 system, javascript1.8.5&&HTML5 version, Dell G3 computer.

Restrict the input input box to only input pure numbers

Use the oninput event, Restrict the input input box to only input pure numbers

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

Code Example

<!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 study: "HTML Video Tutorial"

The above is the detailed content of How to restrict HTML input box to only input pure 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!