The realization of limiting input box to only input pure number in HTML

Time:2021-4-4

limitinputOnly pure numbers can be entered in the input box

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

useonkeyupEvents, yesbugThat is to say, in Chinese input mode, after inputting Chinese characters, press enter directly, and letters will be input directly

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

useonchangeEvent, after input, onlyinputOnly when the focus is lost can we get the result, and can’t respond to the input

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

useoninputEvent, the perfect solution to the above two problems, the test has no other problems.

Code examples

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

So far, this article about the realization of limiting the input box in HTML to only input pure numbers is introduced here. For more related HTML limiting the input of digital content, please search the previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!