只能输入数字和小数点的文本框
Html代码
<html><head><meta http-equiv="content-Type" content="text/html;charset=gb2312"></head><body><!--把下面代码加到<body>与</body>之间-->只能输入数字和小数点的文本框:<input onkeyup="value=value.replace(/[^d.]/g,'')"></body></html>
小数点判断
1 第一个输入必须是数字,不能是小数点。例如.111
2 不能存在多个.连续,只能一个。例如12....1
3 不多出现一个.在不同地方。例如12.1.1
Html代码
<html><head><meta http-equiv="content-Type" content="text/html;charset=gb2312"><title>js 只能输入数字和小数点</title><script language="JavaScript" type="text/javascript"> function clearNoNum(obj) { //先把非数字的都替换掉,除了数字和. obj.value = obj.value.replace(/[^d.]/g,""); //必须保证第一个为数字而不是. obj.value = obj.value.replace(/^./g,""); //保证只有出现一个.而没有多个. obj.value = obj.value.replace(/.{2,}/g,"."); //保证.只出现一次,而不能出现两次以上 obj.value = obj.value.replace(".","$#$").replace(/./g,"").replace("$#$","."); } </script></head><body><!--把下面代码加到<body>与</body>之间-->只能输入数字和小数点的文本框:<input id="input1" onkeyup="clearNoNum(this)"></body></html>