请教各位,有<input type="text" id="txt" name="txt">
;要求txt控件输入的数据为正实数,包括0,最大范围8位整数位,2位小数位。请各位帮忙,谢谢,马上给分。

解决方案 »

  1.   

    <form name="form1">
    <input type="text" id="txt" name="txt" value="12.34">
    <input type="button" onclick="check()" value="Check">
    </form>
    <script>
    function check(){
    var s = document.form1.txt.value;
    if(s.match(/^[1-9][0-9]{0,7}(\.[0-9][0-9]){0,1}$/ig)!=null) alert("ok");
    else alert("error")
    }
    </script>允许有一位小数的话
    if(s.match(/^[1-9][0-9]{0,7}(\.[0-9]{1,2}){0,1}$/ig)!=null) alert("ok");
      

  2.   

    <form name="form1">
    <input type="text" id="txt" name="txt" value="12.34">
    <input type="button" onclick="check()" value="Check">
    </form>
    <script>
    function check(){
    var s = document.form1.txt.value;
    if(s.match(/^(0|[1-9][0-9]{0,7})(\.[0-9]{1,2}){0,1}$/ig)!=null) alert("ok");
    else alert("error")
    }
    </script>
    借用一下楼上的函数,楼上没有包括0,或0.01的情况,或12.3判断错误。
      

  3.   

    jinjuduo给出的更符合要求!
    同样要感谢hookee()