Home > Web Front-end > JS Tutorial > How to limit the input value type of text box in javascript_javascript skills

How to limit the input value type of text box in javascript_javascript skills

WBOY
Release: 2016-05-16 16:00:30
Original
1542 people have browsed it

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>
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template