Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - regular expression to validate date in mm/dd/yyyy format [duplicate]

I'm not good at regular expression. I got code to validate dd/mm/yyyy format which also validates leap year, I tried to modify to get it work for mm/dd/yyyy, but they all failed.

Can some one change it to validate mm/dd/yyyy format?

Regular Expression:

^(((0[1-9]|[12]\d|3[01])/(0[13578]|1[02])/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)/(0[13456789]|1[012])/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])/02/((19|[2-9]\d)\d{2}))|(29/02/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$

Answer Hi All, thanks for the help from all of you, finally parsing again the regular expression, i got my answer to validate mm/dd/yyyy format

Regular Expression:

/^(((0[13578]|1[02])/(0[1-9]|[12]\d|3[01])/((19|[2-9]\d)\d{2}))|((0[13456789]|1[012])/(0[1-9]|[12]\d|30)/((19|[2-9]\d)\d{2}))|(02/(0[1-9]|1\d|2[0-8])/((19|[2-9]\d)\d{2}))|(02/29/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g

like image 538
Sathish Avatar asked Jun 01 '26 15:06

Sathish


1 Answers

Try with

function validateDate(testdate) {
    var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
    return date_regex.test(testdate);
}

But better to use regular expression use Date object from your date string and then validate it.

like image 97
Gautam3164 Avatar answered Jun 04 '26 11:06

Gautam3164



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!