Home >Web Front-end >JS Tutorial >jquery limits the text box to only input numbers, that is, integers and decimals_jquery

jquery limits the text box to only input numbers, that is, integers and decimals_jquery

WBOY
WBOYOriginal
2016-05-16 17:11:381402browse
Copy code The code is as follows:

$(function(){
//The text box can only Enter numbers (excluding decimals), and block input methods and paste
$.fn.integer= function() {
$(this).css("ime-mode", "disabled");
this.bind("keypress",function(e) {
var code = (e.keyCode ? e.keyCode : e.which); //Compatible with Firefox IE
if(!$.browser.msie&& (e.keyCode==0x8)){ //The backspace key cannot be used under Firefox
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*/, '');
}
});
};

//The text box can only enter numbers (including decimals), and blocks the input method and paste
$.fn.number= function() {
$(this).css("ime- mode", "disabled");
this.bind("keypress",function(e) {
var code = (e.keyCode ? e.keyCode : e.which); //Compatible with Firefox IE
if(!$.browser.msie&&(e.keyCode==0x8)){ //The backspace key cannot be used under Firefox
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);
}
});
};
});
Statement:
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