Home > Web Front-end > JS Tutorial > body text

js method to implement a text box that only allows numbers to be entered and limits the size of the numbers_javascript skills

WBOY
Release: 2016-05-16 15:44:19
Original
1436 people have browsed it

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>
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