JS正則表示式 驗證 輸入的是正負小數(保留3位小數)或者只能輸入正負整數
阿新 • • 發佈:2019-02-01
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="jjw" type="text/javascript">
function checknum(obj)
{
var re = /^[+-]?\d*\.?\d{0,3}$/;
if (!re.test(obj.value))
{
alert("非法數字");
obj.value="";
obj.focus();
return false;
}
}
</script>
<input name="rate" type="text"onkeyup="checknum(this)" />
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="jjw" type="text/javascript">
function checknum(obj)
{
var re = /^[+-]?\d*\.?\d{0,3}$/;
if (!re.test(obj.value))
{
alert("非法數字");
obj.value="";
obj.focus();
return false;
}
}
</script>
<input name="rate" type="text"onkeyup="checknum(this)" />
</html>
-----------------------------------------------------------只能輸入正負整數---------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="jjw" type="text/javascript">
function checknum(obj)
{
var re = /^[+-]?\d*$/;
if (!re.test(obj.value))
{
alert("非法數字");
obj.value="";
obj.focus();
return false;
}
}
</script>
<input name="rate" type="text"onkeyup="checknum(this)" />
</html>