当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery限定文本框只能输入数字即整数和小数

jquery限定文本框只能输入数字即整数和小数

2019年03月19日  | 移动技术网IT编程  | 我要评论
代码如下: $(function(){ //文本框只能输入数字(不包括小数),并屏蔽输入法和粘贴 $.fn.integer= function() { $(this).

代码如下:


$(function(){
//文本框只能输入数字(不包括小数),并屏蔽输入法和粘贴
$.fn.integer= function() {
$(this).css("ime-mode", "disabled");
this.bind("keypress",function(e) {
var code = (e.keycode ? e.keycode : e.which); //兼容火狐 ie
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*/, '');
}
});
};

//文本框只能输入数字(包括小数),并屏蔽输入法和粘贴
$.fn.number= function() {
$(this).css("ime-mode", "disabled");
this.bind("keypress",function(e) {
var code = (e.keycode ? e.keycode : e.which); //兼容火狐 ie
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);
}
});
};
});

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网