/**
* 只允许输入数字
* @return
*/
function onlyNum() {
var code;
var evt = document.all ? window.event : arguments.callee.caller.arguments[0];
if(document.all){
code = evt.keyCode;
if(!(code<58&&code>47)&&code!=8&&!(code<106&&code>95))
return code.returnValue = false;
}else{
code = evt.which;
if(!(code<58&&code>47)&&code!=8&&!(code<106&&code>95))
evt.preventDefault();
}
}
//添加keydown事件
onkeydown='javascript:return onlyNum();'
浏览器只允许输入数字
本文介绍了一个简单的JavaScript函数,该函数可以限制用户的输入仅包含数字,适用于各种需要数字输入的表单验证场景。

被折叠的 条评论
为什么被折叠?



