The example in this article describes the method of using js to implement a text box that only allows numbers to be entered and limits the size of the numbers. Share it with everyone for your reference. The details are as follows:
This is a very personalized input box effect. It is stipulated that the text box only allows the input of numbers. If you insist on inputting other characters, the entered characters will automatically disappear unless you enter the specified character format, and There are also restrictions on the size of input numbers! To ensure compatibility, please use Firefox.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-input-limit-num-codes/
The specific code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>首页</title> <style type="text/css"> html,body{margin:0; padding:20px;} </style> </head> <body> <input type="text" id="input" /> <script type="text/javascript"> var text = document.getElementById("input"); text.onkeyup = function(){ this.value=this.value.replace(/\D/g,''); if(text.value>100){ text.value = 100; } } </script> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.