Home>

Let's start with a piece of jquery code that specifies that the text box can only enter numbers including decimals:

<! Doctype html>
<html>
<head>
<meta charset="gb2312">
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
//The text box can only enter numbers (including decimals), and shield the input method and paste
jquery.fn.number=function () {
 this.bind ("keypress", function (e) {
 var code=(e.keycode?e.keycode:e.which);//Compatible with Firefox ie
 //Cannot use backspace key under Firefox
 if (! $. browser.msie &&(e.keycode == 0x8)) {return;}
 if (this.value.indexof (".") ==-1) {return (code>= 48&&code<57) || (code == 46);}
 else {return code>= 48&&code<57}
 });
 this.bind ("paste", function () {return false;});
 this.bind ("keyup", function () {
 if (this.value.slice (0,1) == ".")
 {
  this.value="";
 }
 });
 this.bind ("blur", function () {
 if (this.value.slice (-1) == ".")
 {
  this.value=this.value.slice (0, this.value.length-1);
 }
 });
};
$(function () {
 $("#txt"). number ();
});
</script>
</head>
<body>
<input type="text" />
</body>
</html>

2, jquery how to specify that the text box can only enter integers:

Sometimes the content of a text box can only be a number,And can only be integers,Such as age,You ca n’t fill in 20.8 years old. Here is a code example to explain how to implement this function.I hope to bring help to friends in need,code show as below:

<html>
<head>
<meta charset="gb2312">
<title>ant tribe</title>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
//The text box can only enter numbers (excluding decimals), and shield input methods and paste
jquery.fn.integer=function () {
 this.bind ("keypress", function (e) {
 var code=(e.keycode?e.keycode:e.which);//Compatible with Firefox ie
 //Cannot use backspace key under Firefox
 if (! $. browser.msie &&(e.keycode == 0x8))
 {
  return;
 }
 return code>= 48&&code<57;
 });
 this.bind ("paste", function () {
 return false;
 });
 this.bind ("keyup", function () {
 if (/(^ 0 +) /. test (this.value))
 {
 this.value=this.value.replace (/^ 0 * /, "");
 }
 });
};
$(function () {
 $("#txt"). integer ();
});
</script>
</head>
<body>
<input type="text" />
</body>
</html>

The above code fulfills our requirements,Only integers can be entered in the text box.

  • Previous Substr_count () function in PHP to get the number of occurrences of a substring
  • Next SSL certificate configuration for Nginx server and reverse proxy configuration for SSL