可以输入"2005-5"或"2005-05",也就是只能输入年-月

解决方案 »

  1.   

    /^\d{4}-(0?\d|1[0-2])$/g处理结果 (3)
    2005-05√
    2005-5√
    2005-15
    2005-05-02
    2005-12√
      

  2.   

    不行啊!这是我原来限制只输入数字的表达式,输入错就全消失了,上边的式子不行啊。
    <asp:TextBox id="Seal_Parts" runat="server" Width=6cm onkeyup="value=value.replace(/[^\d\.]/g,'')"/>
      

  3.   

    onkeyup可以检测是不是符合格式,不符合return false,那个输入的字符就取消掉了
    是这么个思路,不知道是不是在onkeyup事件里
      

  4.   

    <!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" >
    <head>
    <title>Untitled Page</title>
    <script type="text/javascript">
    var temp = '';
    function onKeyPress(val) {
        temp = val;
    }
    function onKeyUp(obj) {
        var val = obj.value;
        if (!(/^\d{1,4}$/g.test(val)
            || /^\d{4}\-$/g.test(val)
            || /^\d{4}\-(0?\d|1[0-2])$/g.test(val)
        )) {
            obj.value = temp;
        }
    }
    </script>
    </head>
    <body>
    <input type="text" onkeypress="onKeyPress(this.value)" onKeyUp="doCheck(this);" />
    </body>
    </html>
      

  5.   

    onKeyUp="onKeyUp(this);
    这个地方写错了
      

  6.   


      <asp:TextBox id="Seal_Parts" runat="server" Width=6cm onkeyup="this.value=this.value.replace(/[^(\d{4})(-|\/)(\d{1,2})]/g,'');"/>
      

  7.   

    <input onkeypress="return check(this)">
    <script>
        function check(o){
            var val=String.fromCharCode(event.keyCode);
            if(!/[\d-]/.test(val))return false;
            
            val=o.value+val;
            if(!/^(\d{1,4}|\d{4}[-]|\d{4}[-]\d{1,2})$/.test(val))return false;
            
            return true;
        }
    </script>
      

  8.   


    http://blog.csdn.net/codemon/archive/2010/05/17/5599740.aspx