我想控制输入框只能输入四个字符的数字,如 12.5  12
如果有小数就必须是 .5  只可以是一位小数,最大数不要超过31
请问正侧限制怎么写?
<input size="2" name="Report_date"  maxlength="4" onkeyup="代码?">

解决方案 »

  1.   

    <input  type='text' size="4" name="Report_date" maxlength="4" onblur="/^(?:(?:[1-2]\d?|30?)(\.\d)?)$|^31(?:\.0)?$/.test(this.value)||alert('NO')">
      

  2.   

     <input type='text' size="4" name="Report_date" maxlength="4" onblur="/^(?:(?:[1-2]\d?|30?|0)(\.\d)?)$|^31(?:\.0)?$/.test(this.value)||alert('NO')">
      

  3.   

    谢谢大家,楼上的比较接近,但是还是同上楼的一样存在BUG
    就是输入0.5会出错
    我的值是用于考勤时间的,所以不能大于31,可以等于31
    最小值是0.5
    其他就是如1.5    12    22.5   22   10  之类的,以0.5为倍数!
      

  4.   

    lxcnn老兄的好像可以了,能不能改成如果不符合条件就让输入框的值为0?
      

  5.   

    不一定要正则呀
    <input type='text' size="4" name="Report_date" id="Report_date" maxlength="4" onblur="ck(this.id)" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');if(this.value=='.')this.value='0.';">
    <script>
    function $(id){
    if(!document.getElementById(id)){
    alert("ID:"+id+" 对象不存在!");return false;
    }else{return document.getElementById(id);}
    }
    function ck(id){
      var v=$(id).value;
      if(v%0.5!=0||v<0||v>31||v==""){
        alert("请输入0.5倍数的数!");
        $(id).value=0;
      }
    }
    </script>