javascript method of restricting text box input value types

  • 2020-06-07 04:00:18
  • OfStack

This article illustrated how javascript restricts the type of text box input values. Share to everybody for everybody reference. The specific analysis is as follows:

Requirements: all text box, can only input Numbers and decimal point, other symbols 1 can not input;

The point is that I want to limit what the user inputs, not what the user submits -- that is, if the user enters a number or decimal point in a text box, it works; If you enter non-numeric characters such as letters, the text box will have no response and will not display the characters entered.


<html>
<body>
<script>
var s = "<input type=\"text\" size=\"20\" " +
"style=\"text-align:center\" " +
"onkeydown=\"if(event.keyCode>57&&event.keyCode!=190) return false\" "+ 
// Limits can only be entered to Numbers 
"onblur=\"value=value.replace(/[^0-9\.]/g,'')\" " + 
// Limit mouse clicks to enter non-numbers 
"onbeforepaste=\"clipboardData.setData(\"text\"," + 
// The limit is to paste only Numbers 
"clipboardData.getData(\"text\").replace(/[^0-9\.]/g,''))\">";
document.write(s);
</script>
</body>
</html>

Hopefully, this article has been helpful in your javascript programming.


Related articles: