Regular Expression for MM/DD/YYYY in Javascript

后端 未结 7 1079
青春惊慌失措
青春惊慌失措 2020-12-17 20:47

I\'ve just written this regular expression in javaScript however it doesn\'t seem to work, here\'s my function:

function isGoodDate(dt){
    var reGoodDate =         


        
7条回答
  •  天命终不由人
    2020-12-17 21:29

    Try the below code which accepts following date formats:

    MM-DD-YYYY, MM-DD-YY, DD-MM-YYYY, DD-MM-YY, MM/DD/YYYY, MM/DD/YY, DD/MM/YYYY, DD/MM/YY, MM\DD\YYYY, MM\DD\YY, DD\MM\YYYY, DD\MM\YY

    function isGoodDate(dt) {
        var reGoodDate = /(?:((0\d|[12]\d|3[01])|(0\d|1[012]))[\-|\\|\/]((0\d|1[012])|(0\d|[12]\d|3[01]))[\-|\\|\/](((19|20)\d{2})|\d\d))/;
        return reGoodDate.test(dt);
    }
    

提交回复
热议问题