JS限制只能输入数字
时间:2010-09-23 来源:Simcoder
代码
function filterInt(evt){
evt = evt || window.event;
var b = evt.keyCode || evt.which;
return(b>=48&&b<=57)||b==44||b==45;
}
function filterFloat( evt ){
evt = evt || window.event;
var b = evt.keyCode || evt.which;
var _el = evt.srcElement || evt.target;
if( b == 46 ){
var _val = _el.value;
if( _val == "" || _val.split(".").length == 2 ){
return false;
}
}
return(b>=48&&b<=57)||b==44||b==45||b==46;
}
限制输入整型: <input type="text" onkeypress="return filterInt(event);"/>
限制输入浮点型: <input type="text" onkeypress="return filterFloat(event);"/>
evt = evt || window.event;
var b = evt.keyCode || evt.which;
return(b>=48&&b<=57)||b==44||b==45;
}
function filterFloat( evt ){
evt = evt || window.event;
var b = evt.keyCode || evt.which;
var _el = evt.srcElement || evt.target;
if( b == 46 ){
var _val = _el.value;
if( _val == "" || _val.split(".").length == 2 ){
return false;
}
}
return(b>=48&&b<=57)||b==44||b==45||b==46;
}
限制输入整型: <input type="text" onkeypress="return filterInt(event);"/>
限制输入浮点型: <input type="text" onkeypress="return filterFloat(event);"/>
相关阅读 更多 +