Validating Date Format Using Regular Expression

Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support

The regex you pasted does not validate leap years correctly, but there is one that does in the same post.
I modified it to take dd/mm/yyyy, dd-mm-yyyy or dd.mm.yyyy.

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

I tested it a bit in the link Arun provided in his answer and also here and it seems to work.

Edit February 14th 2019: I've removed a comma that was in the regex which allowed dates like 29-0,-11

Regex to validate date format

For a month from 1-12 adding a zero for a single digit 1-9:

(?:0[1-9]|1[012])

For a day 1-31 adding a zero for a single digit 1-9:

(?:0[1-9]|[12]\d|3[01])

Putting it all together with 4 digits to match a year (note that \d{4} can also match 0000 and 9999), enclosed in word boundaries \b to prevent a partial match with leading or trailing digits / word characters:

\b\d{4}(?:0[1-9]|1[012])(?:0[1-9]|[12]\d|3[01])\b

A variation, limiting the scope for a year to for example 1900 - 2099

\b(?:19|20)\d{2}(?:0[1-9]|1[012])(?:0[1-9]|[12]\d|3[01])\b

But note that this does not validate the date itself, it can for example also match 20210231. To validate a date, use a designated api for handling a date in the tool or code.

Regex to match date formats DD-MM-YYYY and DD/MM/YYYY

This pattern will work for your conditon. Strictly not allowed month to date and vice versa.

^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$

This pattern will match these formats: (Date, Month, Year)

25.04.2017  
02.04.2017
2.4.2017

25/04/2017
5/12/2017
15/2/2017

25-04-2017
6-10-2017
16-5-2017

Regular Expression to match valid dates

This is not an appropriate use of regular expressions. You'd be better off using

[0-9]{2}/[0-9]{2}/[0-9]{4}

and then checking ranges in a higher-level language.

Javascript - Regex to validate date format

You could use a character class ([./-]) so that the seperators can be any of the defined characters

var dateReg = /^\d{2}[./-]\d{2}[./-]\d{4}$/

Or better still, match the character class for the first seperator, then capture that as a group ([./-]) and use a reference to the captured group \1 to match the second seperator, which will ensure that both seperators are the same:

var dateReg = /^\d{2}([./-])\d{2}\1\d{4}$/

"22-03-1981".match(dateReg) // matches
"22.03-1981".match(dateReg) // does not match
"22.03.1981".match(dateReg) // matches

how to validate date format using regular expression

try the following (It replaces the dashes with commas):

\d{1,2}/\d{1,2}/\d{4}

How to validate date format with/without zeros?

You could make the 0 optional, add 2 digits \d\d after matching 19 or 20 and use word boundaries \b.

If you don't need the capturing groups, you can use non capturing (?: groups`

\b(?:0?[1-9]|1[012])[- /.](?:0?[1-9]|[12][0-9]|3[01])[- /.](?:19|20)\d\d\b

Regex demo

If you want to have consistent delimiters, you could make use of a capturing group and a backreference \1 to what is captured in the group to not match for example 3/04-1991

\b(?:0?[1-9]|1[012])([- /.])(?:0?[1-9]|[12][0-9]|3[01])\1(?:19|20)\d\d\b
^ ^ ^^

Regex demo



Related Topics



Leave a reply



Submit