×

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

作者:Terry2017.02.15来源:Web前端之家浏览:10088评论:0
关键词:JQueryJS
复制代码 代码如下:

$(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);
}
});
};
});

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/svg1487088000994.html

网友评论文明上网理性发言已有0人参与

发表评论: