Skip to content Skip to sidebar Skip to footer

Regular Expression For Date Format Mm/dd/yyyy Returns False Results

I have a jquery date picker that pops up everytime I click my textbox(prfm_dtrequest). It returns mm/dd/yyyy format which is the way I want it to be. I need to validate the content

Solution 1:

Please correct your regex (valid date example: 03/31/1988):

$(document).ready(function(){
    $("#submitform").click(function(){

    /* isValidData source: http://stackoverflow.com/questions/5812220/how-to-validate-a-date */functionisValidDate(s) {
       var bits = s.split('/');
       var d = newDate(bits[2], bits[0] - 1, bits[1]);
       return d && (d.getMonth() + 1) == bits[0] && d.getDate() == Number(bits[1]);
    }

    var daterequested = $("#prfm_dtrequest").val();
        var dateMMDDYYYRegex = "^[0-9]{2}/[0-9]{2}/[0-9]{4}$";
        if(daterequested.match(dateMMDDYYYRegex) && isValidDate(daterequested)){
            alert('is a valid date: '+daterequested);
        }else{
            alert('is not a valid date: '+daterequested);
        }
    });
});

Demo

Result:

enter image description here

Solution 2:

Use below regex for mm/dd/yyyy format:

var dateMMDDYYYRegex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
    if(dateMMDDYYYRegex.test(dateMMDDYYYRegex)){
       alert('is a valid date: '+daterequested);
    }else{
       alert('is not a valid date: '+daterequested);
    }

Post a Comment for "Regular Expression For Date Format Mm/dd/yyyy Returns False Results"