<ul>
<li><span>2位小数(默认):</span><input type="text" id="ttext" class="ime" οnkeypress="return myNumberic(event)" /></li>
<li><span>0位小数:</span><input type="text" id="ttext2" class="ime" οnkeypress="return myNumberic(event,0)" /></li>
<li><span>1位小数:</span><input type="text" id="ttext3" class="ime" οnkeypress="return myNumberic(event,1)" /></li>
</ul>
</div>
</body>
<script type="text/javascript" src="../sign.js" defer="defer"></script>
<script type="text/javascript">
function myNumberic(e,len) {
var obj=e.srcElement || e.target;
var dot=obj.value.indexOf(".");//alert(e.which);
len =(typeof(len)=="undefined")?2:len;
var key=e.keyCode|| e.which;
if(key==8 || key==9 || key==46 || (key>=37 && key<=40))//这里为了兼容Firefox的backspace,tab,del,方向键
return true;
if (key<=57 && key>=48) { //数字
if(dot==-1)//没有小数点
return true;
else if(obj.value.length<=dot+len)//小数位数
return true;
} else if((key==46) && dot==-1){//小数点
return true;
}
return false;
}
</script>
个人感觉很好 ,就从sohighthesky里转了过来
关于判断只能输入数字的函数
最新推荐文章于 2024-01-09 10:38:06 发布