Limit input box can only enter pure digital input

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

Use onkeyup event, there are bug, that is, in the Chinese input method state, after input Chinese characters directly enter, will enter the letter directly;

 

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

Use onchange event, after typing in, lose focus when only input will get results, not just respond when input

 

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

Use oninput event, the perfect solution to these two problems, the test had no other problems.

 

 The sample code

<!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,'')">
</body>
</html>
The code above tests have been completed at Google, Firefox, Baidu, UC, IE11,360 rapid, QQ, Edge browser, ease of use!  
Original link: https://blog.csdn.net/w6990548/article/details/79388905

Guess you like

Origin www.cnblogs.com/Salicejy/p/10968295.html