<script>
function check(ss){
if (/\-0/.test(ss)) return false;
re=/^(\d{2})\-(\d{1,2})\-(\d{1,2})$/.exec(ss);
if (!re) return false;
return ((re[2]>0) && (re[2]<13) && (re[3]>0) && (re[3]<32))
}alert(check("99-08-01"))
alert(check("99-8-1"))
</script>

解决方案 »

  1.   

    http://www.blueidea.com/bbs/archivecontent.asp?id=472344<script>
    function strDate(str){
    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; 
    var r = str.match(reg); 
    if(r==null)return false; 
    var d= new Date(r[1], r[3]-1,r[4]); 
    var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
    return newStr==str
    }
    alert(strDate("2002-1-31"))
    alert(strDate("2002-2-31"))
    alert(strDate("2002-1-41"))
    </script>
      

  2.   

    多谢!能否详细解释一下这个匹配模式字符串的用法,这个问题一直困惑俺很久了。
    /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$//^  和   $/是否为开始结尾符号。(\d{1,4})表示4个整数数字?
    (-|\/)表示-  或者 /?
    (\d{1,2}) 表示2个整数数字?
    \2表示什么?
      

  3.   

    用split()可以,就是麻烦多了,但是正刚表达确实简单解决,不过看起来跟乱码一样,我要学,我要背,我要努力
      

  4.   

    多谢!那下面这些语句应作何解?/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/var r = str.match(reg);    
    通过匹配,r获得一个4元的数组?从左往右为r[1],r[2],r[3],r[4]?
    如此,r[3]就应该为月份,为何要减1 if(r==null)return false; 
    var d= new Date(r[1], r[3]-1,r[4]); 
    var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
    return newStr==str