The example in this article describes the method of restricting the input value type of the text box in JavaScript. Share it with everyone for your reference. The specific analysis is as follows:
Requirement: In all text boxes, only numbers and decimal points can be entered, and no other symbols can be entered;
The key point is that I want to limit it when the user inputs, rather than judging when submitting - that is to say, if the user enters numbers or decimal points in the text box, they can enter normally; if they enter non-numeric characters such as letters, the text The box will be unresponsive and will not display the entered characters.
<html> <body> <script> var s = "<input type=\"text\" size=\"20\" " + "style=\"text-align:center\" " + "onkeydown=\"if(event.keyCode>57&&event.keyCode!=190) return false\" "+ //限制只能输入数字 "onblur=\"value=value.replace(/[^0-9\.]/g,'')\" " + //限制鼠标点击输入非数字 "onbeforepaste=\"clipboardData.setData(\"text\"," + //限制只能粘贴数字 "clipboardData.getData(\"text\").replace(/[^0-9\.]/g,''))\">"; document.write(s); </script> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.