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 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
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. How to write regex to validate dates?
I came up with this:
function isValidDate(inputDate){
var myRegex = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/;
var match = myRegex.exec(inputDate);
if (match != null) {
var auxDay = match[1];
var auxMonth = match[3] - 1;
var auxYear = match[4];
auxYear = auxYear.length < 3 ? (auxYear < 70 ? '20' + auxYear : '19' + auxYear) : auxYear;
var testingDate = new Date(auxYear,auxMonth,auxDay);
return ((auxDay == testingDate.getDate()) && (auxMonth == testingDate.getMonth()) && (auxYear == testingDate.getFullYear()));
} else return false;
}
Works for dd-mm-yyyy
, dd-mm-yy
, d-m-yyyy
and d-m-yy
, using -
or /
as separatorsBased on This script
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
Date of birth validation by using regular expression
For regex for dates, see the link: Regex Tutorial
But I think the example will work.
"^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\\d\\d$"
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. regular expression to validate datetime format (MM/DD/YYYY)
Try your regex with a tool like http://jsregex.com/ (There is many) or better, a unit test.
For a native validation:
function validateDate(testdate) {
var date_regex = /^\d{2}\/\d{2}\/\d{4}$/ ;
return date_regex.test(testdate);
}
In your case, to validate (MM/DD/YYYY), with a year between 1900 and 2099, I'll write it like that: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);
}
Date validation with regular expression
You can select 1 or 2 digit occurrences with
\d{1,2}
.There are missing square brackets in the last hypen / forward slash group.
And the last group should be
(\d{2}|\d{4})
.No need to escape the forward slash inside the character classes.
/^(\d{1,2})([\-/])(\d{1,2})([\-/])(\d{2}|\d{4})$/
Related Topics
How to Access Nested JSON Data
What's Wrong with Var X = New Array();
JavaScript Replace() Method Dollar Signs
Getusermedia() in Chrome 47 Without Using Https
How to Share States Between Components Using the Usestate() Hook in React
Viewing All the Timeouts/Intervals in JavaScript
How to Close a Window with JavaScript on Mozilla Firefox 3
Merge Duplicate Objects in Array of Objects
Google Spreadsheet Script - How to Transpose/Rotate Multi-Dimensional Array
Rest with Express.Js Nested Router
How to Add a "Readonly" Attribute to an <Input>
What Is #Auto Attribute Here and Why It Is Required
Localstorage Object Is Undefined in Ie
Gmail Extension, Sendmessage to Background from Page Context
Can Jquery Read/Write Cookies to a Browser
Load Local JSON File into Variable