input输⼊框限制输⼊的数值类型<html>
<head>
<script language="JavaScript">
function onlyNumber(obj){
//得到第⼀个字符是否为负号
var t = obj.value.charAt(0);
//先把⾮数字的都替换掉,除了数字和.
input绑定onblur事件
obj.value = place(/[^\d\.]/g,'');
//必须保证第⼀个为数字⽽不是.
obj.value = place(/^\./g,'');
//保证只有出现⼀个.⽽没有多个.
obj.value = place(/\.{2,}/g,'.');
//保证.只出现⼀次,⽽不能出现两次以上
obj.value = place('.','$#$').replace(/\./g,'').replace('$#$','.');
//如果第⼀位是负号,则允许添加
if(t == '-'){
obj.value = '-'+obj.value;
}
}
</script>
<meta http-equiv="content-Type" content="text/html;charset=gb2312">
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<title>限制⽂本框只能输⼊数字||只能是数字和⼩数点||只能是整数和浮点数</title>
</head>
<body>
只能输⼊数字的⽂本框:
<input onkeyup="this.value=place(/\D/g,'')" onblur="this.value=place(/\D/g,'')"/>
只能输⼊数字和⼩数点的⽂本框:
<input onkeyup="place(/[^\d\.]/g,'')" onblur="place(/[^\d\.]/g,'')"/>
只能输⼊数字且只能有⼀个⼩数点的⽂本框(⼩数点不能在开头,可以在结尾,第⼀位允许添加负号即浮点数):
<input onkeyup="onlyNumber(this)" onblur="onlyNumber(this)"/>
</body>
</html>