<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<input type="text" id ="test" name="test" οnkeyup="clearNoNum(this)"/>
<script>
function clearNoNum(obj){
obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
obj.value = obj.value.replace(/^00/g,""); //验证前两位不能同时为0
obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/,'$1$2.$3');
}
</script>
</body>
</html>
<html>
<body>
<h1>My First Web Page</h1>
<input type="text" id ="test" name="test" οnkeyup="clearNoNum(this)"/>
<script>
function clearNoNum(obj){
obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
obj.value = obj.value.replace(/^00/g,""); //验证前两位不能同时为0
obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/,'$1$2.$3');
}
</script>
</body>
</html>