来源:http://topic.csdn.net/t/20050127/19/3759043.html
请问如何用正则表达式检查一个输入值,输入值可以为负数,0,正数,小数点后面可以最多有四位?
答案1:
<script language="javascript">
function check(str)
{
var rgx=/^([/-]?)([0-9]+)(/.[0-9]{1,4})?$/;
if(rgx.test(str))
{
alert('匹配');
}
else
{
alert('不匹配');
}
}
</script>
<body>
<form name=form1 >
<input type=text name="a">
<input type=button οnclick='check(document.form1.a.value)' value="click">
</form>
</body>
答案2:
/^(0|-?[1-9]/d*(/./d{1,4})?)$/