Javascript date regex DD/MM/YYYY
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I know there are a lot of regex threads out there by I need a specific pattern I couldn't fin anywhere
This regex validates in a YYYY-MM-DD format
/^d{4}[/-](0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01])$/
I need the pattern to be DD/MM/YYYY
(day first since it's in spanish and only "/", "-" should not be allowed)
I searched several regex libraries and I think this one should work... but since I'm not familiar with regex I'm not sure it validates like that
(0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd
I also don't know ho to escape the slashes, I try to "see" the logic in the string but it's like trying "see" the Matrix code for me. I'm placing the regex string in a options .js
[...] },
"date": {
"regex": (0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd,
"alertText": "Alert text AAAA-MM-DD"
},
"other type..."[...]
So, if the regex is ok, how would I escape it?
if it's not, what's the correct regex and how do I escape it? :P
Thanks a lot
javascript regex
add a comment |
I know there are a lot of regex threads out there by I need a specific pattern I couldn't fin anywhere
This regex validates in a YYYY-MM-DD format
/^d{4}[/-](0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01])$/
I need the pattern to be DD/MM/YYYY
(day first since it's in spanish and only "/", "-" should not be allowed)
I searched several regex libraries and I think this one should work... but since I'm not familiar with regex I'm not sure it validates like that
(0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd
I also don't know ho to escape the slashes, I try to "see" the logic in the string but it's like trying "see" the Matrix code for me. I'm placing the regex string in a options .js
[...] },
"date": {
"regex": (0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd,
"alertText": "Alert text AAAA-MM-DD"
},
"other type..."[...]
So, if the regex is ok, how would I escape it?
if it's not, what's the correct regex and how do I escape it? :P
Thanks a lot
javascript regex
3
Are you sure regex is the best option here?
– Alex
Mar 28 '11 at 21:39
1
Possible duplicate of Regex to validate date format dd/mm/yyyy
– rene
Sep 3 '16 at 19:34
add a comment |
I know there are a lot of regex threads out there by I need a specific pattern I couldn't fin anywhere
This regex validates in a YYYY-MM-DD format
/^d{4}[/-](0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01])$/
I need the pattern to be DD/MM/YYYY
(day first since it's in spanish and only "/", "-" should not be allowed)
I searched several regex libraries and I think this one should work... but since I'm not familiar with regex I'm not sure it validates like that
(0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd
I also don't know ho to escape the slashes, I try to "see" the logic in the string but it's like trying "see" the Matrix code for me. I'm placing the regex string in a options .js
[...] },
"date": {
"regex": (0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd,
"alertText": "Alert text AAAA-MM-DD"
},
"other type..."[...]
So, if the regex is ok, how would I escape it?
if it's not, what's the correct regex and how do I escape it? :P
Thanks a lot
javascript regex
I know there are a lot of regex threads out there by I need a specific pattern I couldn't fin anywhere
This regex validates in a YYYY-MM-DD format
/^d{4}[/-](0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01])$/
I need the pattern to be DD/MM/YYYY
(day first since it's in spanish and only "/", "-" should not be allowed)
I searched several regex libraries and I think this one should work... but since I'm not familiar with regex I'm not sure it validates like that
(0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd
I also don't know ho to escape the slashes, I try to "see" the logic in the string but it's like trying "see" the Matrix code for me. I'm placing the regex string in a options .js
[...] },
"date": {
"regex": (0[1-9]|[12][0-9]|3[01])[ .-](0[1-9]|1[012])[ .-](19|20|)dd,
"alertText": "Alert text AAAA-MM-DD"
},
"other type..."[...]
So, if the regex is ok, how would I escape it?
if it's not, what's the correct regex and how do I escape it? :P
Thanks a lot
javascript regex
javascript regex
asked Mar 28 '11 at 21:37
Juan IgnacioJuan Ignacio
1,56272552
1,56272552
3
Are you sure regex is the best option here?
– Alex
Mar 28 '11 at 21:39
1
Possible duplicate of Regex to validate date format dd/mm/yyyy
– rene
Sep 3 '16 at 19:34
add a comment |
3
Are you sure regex is the best option here?
– Alex
Mar 28 '11 at 21:39
1
Possible duplicate of Regex to validate date format dd/mm/yyyy
– rene
Sep 3 '16 at 19:34
3
3
Are you sure regex is the best option here?
– Alex
Mar 28 '11 at 21:39
Are you sure regex is the best option here?
– Alex
Mar 28 '11 at 21:39
1
1
Possible duplicate of Regex to validate date format dd/mm/yyyy
– rene
Sep 3 '16 at 19:34
Possible duplicate of Regex to validate date format dd/mm/yyyy
– rene
Sep 3 '16 at 19:34
add a comment |
11 Answers
11
active
oldest
votes
I use this function for dd/mm/yyyy format :
// (new Date()).fromString("3/9/2013") : 3 of september
// (new Date()).fromString("3/9/2013", false) : 9 of march
Date.prototype.fromString = function(str, ddmmyyyy) {
var m = str.match(/(d+)(-|/)(d+)(?:-|/)(?:(d+)s+(d+):(d+)(?::(d+))?(?:.(d+))?)?/);
if(m[2] == "/"){
if(ddmmyyyy === false)
return new Date(+m[4], +m[1] - 1, +m[3], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
return new Date(+m[4], +m[3] - 1, +m[1], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
return new Date(+m[1], +m[3] - 1, +m[4], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
add a comment |
You could take the regex that validates YYYY/MM/DD and flip it around to get what you need for DD/MM/YYYY:
/^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]d{4}$/
BTW - this regex validates for either DD/MM/YYYY or DD-MM-YYYY
P.S. This will allow dates such as 31/02/4899
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
7
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
4
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to acceptDD/MM/YYYY
and notDD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
|
show 1 more comment
A regex is good for matching the general format but I think you should move parsing to the Date class, e.g.:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
Now you can use this function to check for valid dates; however, if you need to actually validate without rolling (e.g. "31/2/2010" doesn't automatically roll to "3/3/2010") then you've got another problem.
[Edit] If you also want to validate without rolling then you could add a check to compare against the original string to make sure it is the same date:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, nonRolling = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (nonRolling) ? d : null;
}
[Edit2] If you want to match against zero-padded dates (e.g. "08/08/2013") then you could do something like this:
function parseDate(str) {
function pad(x){return (((''+x).length==2) ? '' : '0') + x; }
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, matchesPadded = (d&&(str==[pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/')))
, matchesNonPadded = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (matchesPadded || matchesNonPadded) ? d : null;
}
However, it will still fail for inconsistently padded dates (e.g. "8/08/2013").
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
1
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
No need to rebuild the string. Justd.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
add a comment |
Take a look from here http://forums.asp.net/t/1410702.aspx/1
Use this following Regular Expression Details, This will support leap year also.
var reg = /^(((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]|1d|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))))$/g;
Example
2
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
1
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
1
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
add a comment |
Scape slashes is simply use before
/
and it will be escaped. (/
=> /
).
Otherwise you're regex DD/MM/YYYY could be next:
/^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/g
Explanation:
[0-9]
: Just Numbers
{2}
or{4}
: Length 2 or 4. You could do{2,4}
as well to length between two numbers (2 and 4 in this case)
[/]
: Character/
g
: Global -- Orm
: Multiline (Optional, see your requirements)
$
: Anchor to end of string. (Optional, see your requirements)
^
: Start of string. (Optional, see your requirements)
An example of use:
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
Of course you can use as boolean:
if(regex.test(date)){
//do something
}
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
add a comment |
Try using this..
[0-9]{2}[/][0-9]{2}[/][0-9]{4}$
this should work with this pattern DD/DD/DDDD
where D is any digit (0-9)
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
add a comment |
If you are in Javascript already, couldn't you just use Date.Parse() to validate a date instead of using regEx.
RegEx for date is actually unwieldy and hard to get right especially with leap years and all.
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
add a comment |
((?=d{4})d{4}|(?=[a-zA-Z]{3})[a-zA-Z]{3}|d{2})((?=/)/|-)((?=[0-9]{2})[0-9]{2}|(?=[0-9]{1,2})[0-9]{1,2}|[a-zA-Z]{3})((?=/)/|-)((?=[0-9]{4})[0-9]{4}|(?=[0-9]{2})[0-9]{2}|[a-zA-Z]{3})
Regex Compile on it
2012/22/Jan
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012-Dec-22
2012-12-22
23/12/2012
23/12/2012
Dec-22-2012
12-2-2012
23-12-2012
23-12-2012
add a comment |
For people who needs to validate years earlier than year 1900, following should do the trick. Actually this is same as the above answer given by [@OammieR][1]
BUT with years including 1800 - 1899.
/^(((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((18|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((18|[2-9]d)d{2}))|((0[1-9]|1d|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))))$/
Hope this helps someone who needs to validate years earlier than 1900, such as 01/01/1855
, etc.
Thanks @OammieR
for the initial idea.
add a comment |
Do the following change to the jquery.validationengine-en.js
file and update the dd/mm/yyyy inline validation by including leap year:
"date": {
// Check if date is valid by leap year
"func": function (field) {
//var pattern = new RegExp(/^(d{4})[/-.](0?[1-9]|1[012])[/-.](0?[1-9]|[12][0-9]|3[01])$/);
var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[/-.](0?[1-9]|1[012])[/-.](d{4})$/);
var match = pattern.exec(field.val());
if (match == null)
return false;
//var year = match[1];
//var month = match[2]*1;
//var day = match[3]*1;
var year = match[3];
var month = match[2]*1;
var day = match[1]*1;
var date = new Date(year, month - 1, day); // because months starts from 0.
return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day);
},
"alertText": "* Invalid date, must be in DD-MM-YYYY format"
add a comment |
I build this regular to check month 30/31 and let february to 29.
new RegExp(/^((0[1-9]|[12][0-9]|3[01])(/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(/)(02))|((0[1-9]|[12][0-9]|3[0])(/)(0[469]|11))(/)d{4}$/)
I think, it's more simple and more flexible and enough full.
Perhaps first part can be contract but I Don't find properly.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5465375%2fjavascript-date-regex-dd-mm-yyyy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
I use this function for dd/mm/yyyy format :
// (new Date()).fromString("3/9/2013") : 3 of september
// (new Date()).fromString("3/9/2013", false) : 9 of march
Date.prototype.fromString = function(str, ddmmyyyy) {
var m = str.match(/(d+)(-|/)(d+)(?:-|/)(?:(d+)s+(d+):(d+)(?::(d+))?(?:.(d+))?)?/);
if(m[2] == "/"){
if(ddmmyyyy === false)
return new Date(+m[4], +m[1] - 1, +m[3], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
return new Date(+m[4], +m[3] - 1, +m[1], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
return new Date(+m[1], +m[3] - 1, +m[4], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
add a comment |
I use this function for dd/mm/yyyy format :
// (new Date()).fromString("3/9/2013") : 3 of september
// (new Date()).fromString("3/9/2013", false) : 9 of march
Date.prototype.fromString = function(str, ddmmyyyy) {
var m = str.match(/(d+)(-|/)(d+)(?:-|/)(?:(d+)s+(d+):(d+)(?::(d+))?(?:.(d+))?)?/);
if(m[2] == "/"){
if(ddmmyyyy === false)
return new Date(+m[4], +m[1] - 1, +m[3], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
return new Date(+m[4], +m[3] - 1, +m[1], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
return new Date(+m[1], +m[3] - 1, +m[4], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
add a comment |
I use this function for dd/mm/yyyy format :
// (new Date()).fromString("3/9/2013") : 3 of september
// (new Date()).fromString("3/9/2013", false) : 9 of march
Date.prototype.fromString = function(str, ddmmyyyy) {
var m = str.match(/(d+)(-|/)(d+)(?:-|/)(?:(d+)s+(d+):(d+)(?::(d+))?(?:.(d+))?)?/);
if(m[2] == "/"){
if(ddmmyyyy === false)
return new Date(+m[4], +m[1] - 1, +m[3], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
return new Date(+m[4], +m[3] - 1, +m[1], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
return new Date(+m[1], +m[3] - 1, +m[4], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
I use this function for dd/mm/yyyy format :
// (new Date()).fromString("3/9/2013") : 3 of september
// (new Date()).fromString("3/9/2013", false) : 9 of march
Date.prototype.fromString = function(str, ddmmyyyy) {
var m = str.match(/(d+)(-|/)(d+)(?:-|/)(?:(d+)s+(d+):(d+)(?::(d+))?(?:.(d+))?)?/);
if(m[2] == "/"){
if(ddmmyyyy === false)
return new Date(+m[4], +m[1] - 1, +m[3], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
return new Date(+m[4], +m[3] - 1, +m[1], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
return new Date(+m[1], +m[3] - 1, +m[4], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}
edited Sep 23 '13 at 20:12
answered Sep 23 '13 at 19:46
edidedid
23436
23436
add a comment |
add a comment |
You could take the regex that validates YYYY/MM/DD and flip it around to get what you need for DD/MM/YYYY:
/^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]d{4}$/
BTW - this regex validates for either DD/MM/YYYY or DD-MM-YYYY
P.S. This will allow dates such as 31/02/4899
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
7
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
4
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to acceptDD/MM/YYYY
and notDD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
|
show 1 more comment
You could take the regex that validates YYYY/MM/DD and flip it around to get what you need for DD/MM/YYYY:
/^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]d{4}$/
BTW - this regex validates for either DD/MM/YYYY or DD-MM-YYYY
P.S. This will allow dates such as 31/02/4899
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
7
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
4
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to acceptDD/MM/YYYY
and notDD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
|
show 1 more comment
You could take the regex that validates YYYY/MM/DD and flip it around to get what you need for DD/MM/YYYY:
/^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]d{4}$/
BTW - this regex validates for either DD/MM/YYYY or DD-MM-YYYY
P.S. This will allow dates such as 31/02/4899
You could take the regex that validates YYYY/MM/DD and flip it around to get what you need for DD/MM/YYYY:
/^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]d{4}$/
BTW - this regex validates for either DD/MM/YYYY or DD-MM-YYYY
P.S. This will allow dates such as 31/02/4899
answered Mar 28 '11 at 21:42
mVChrmVChr
41.1k78592
41.1k78592
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
7
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
4
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to acceptDD/MM/YYYY
and notDD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
|
show 1 more comment
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
7
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
4
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to acceptDD/MM/YYYY
and notDD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
I tried flip it around but I wasn't sure where did each block started. Can this regex be edited to allow / / but not - - ? Thanks!
– Juan Ignacio
Mar 30 '11 at 4:32
7
7
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
fails on valid Feb 29s
– mplungjan
Dec 14 '12 at 14:00
4
4
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
it also works on DD/MM-YYYY
– Fallenreaper
Jan 29 '13 at 14:47
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to accept DD/MM/YYYY
and not DD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/-]d{4}$
Regex if you just want to accept DD/MM/YYYY
and not DD-MM-YYYY
– lukehillonline
Aug 23 '16 at 9:01
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
No match on 06/20/2016 either.
– Paul
Sep 15 '16 at 16:31
|
show 1 more comment
A regex is good for matching the general format but I think you should move parsing to the Date class, e.g.:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
Now you can use this function to check for valid dates; however, if you need to actually validate without rolling (e.g. "31/2/2010" doesn't automatically roll to "3/3/2010") then you've got another problem.
[Edit] If you also want to validate without rolling then you could add a check to compare against the original string to make sure it is the same date:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, nonRolling = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (nonRolling) ? d : null;
}
[Edit2] If you want to match against zero-padded dates (e.g. "08/08/2013") then you could do something like this:
function parseDate(str) {
function pad(x){return (((''+x).length==2) ? '' : '0') + x; }
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, matchesPadded = (d&&(str==[pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/')))
, matchesNonPadded = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (matchesPadded || matchesNonPadded) ? d : null;
}
However, it will still fail for inconsistently padded dates (e.g. "8/08/2013").
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
1
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
No need to rebuild the string. Justd.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
add a comment |
A regex is good for matching the general format but I think you should move parsing to the Date class, e.g.:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
Now you can use this function to check for valid dates; however, if you need to actually validate without rolling (e.g. "31/2/2010" doesn't automatically roll to "3/3/2010") then you've got another problem.
[Edit] If you also want to validate without rolling then you could add a check to compare against the original string to make sure it is the same date:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, nonRolling = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (nonRolling) ? d : null;
}
[Edit2] If you want to match against zero-padded dates (e.g. "08/08/2013") then you could do something like this:
function parseDate(str) {
function pad(x){return (((''+x).length==2) ? '' : '0') + x; }
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, matchesPadded = (d&&(str==[pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/')))
, matchesNonPadded = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (matchesPadded || matchesNonPadded) ? d : null;
}
However, it will still fail for inconsistently padded dates (e.g. "8/08/2013").
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
1
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
No need to rebuild the string. Justd.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
add a comment |
A regex is good for matching the general format but I think you should move parsing to the Date class, e.g.:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
Now you can use this function to check for valid dates; however, if you need to actually validate without rolling (e.g. "31/2/2010" doesn't automatically roll to "3/3/2010") then you've got another problem.
[Edit] If you also want to validate without rolling then you could add a check to compare against the original string to make sure it is the same date:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, nonRolling = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (nonRolling) ? d : null;
}
[Edit2] If you want to match against zero-padded dates (e.g. "08/08/2013") then you could do something like this:
function parseDate(str) {
function pad(x){return (((''+x).length==2) ? '' : '0') + x; }
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, matchesPadded = (d&&(str==[pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/')))
, matchesNonPadded = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (matchesPadded || matchesNonPadded) ? d : null;
}
However, it will still fail for inconsistently padded dates (e.g. "8/08/2013").
A regex is good for matching the general format but I think you should move parsing to the Date class, e.g.:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
Now you can use this function to check for valid dates; however, if you need to actually validate without rolling (e.g. "31/2/2010" doesn't automatically roll to "3/3/2010") then you've got another problem.
[Edit] If you also want to validate without rolling then you could add a check to compare against the original string to make sure it is the same date:
function parseDate(str) {
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, nonRolling = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (nonRolling) ? d : null;
}
[Edit2] If you want to match against zero-padded dates (e.g. "08/08/2013") then you could do something like this:
function parseDate(str) {
function pad(x){return (((''+x).length==2) ? '' : '0') + x; }
var m = str.match(/^(d{1,2})/(d{1,2})/(d{4})$/)
, d = (m) ? new Date(m[3], m[2]-1, m[1]) : null
, matchesPadded = (d&&(str==[pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/')))
, matchesNonPadded = (d&&(str==[d.getDate(),d.getMonth()+1,d.getFullYear()].join('/')));
return (matchesPadded || matchesNonPadded) ? d : null;
}
However, it will still fail for inconsistently padded dates (e.g. "8/08/2013").
edited Aug 2 '13 at 15:19
answered Mar 28 '11 at 21:48
maericsmaerics
107k30205251
107k30205251
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
1
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
No need to rebuild the string. Justd.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
add a comment |
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
1
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
No need to rebuild the string. Justd.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
Nice idea! Only problem I found was '08/08/2013' won't pass the nonRolling validation because of the zeros.
– Chris Haines
Aug 2 '13 at 10:03
1
1
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
@Hainesy: yes, good point, see my updated answer.
– maerics
Aug 2 '13 at 15:19
No need to rebuild the string. Just
d.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
No need to rebuild the string. Just
d.getDate() == m[1] && d.getMonth() == m[2] && d.getFullYear() == m[3]
– Richard Ayotte
May 24 '15 at 19:25
add a comment |
Take a look from here http://forums.asp.net/t/1410702.aspx/1
Use this following Regular Expression Details, This will support leap year also.
var reg = /^(((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]|1d|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))))$/g;
Example
2
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
1
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
1
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
add a comment |
Take a look from here http://forums.asp.net/t/1410702.aspx/1
Use this following Regular Expression Details, This will support leap year also.
var reg = /^(((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]|1d|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))))$/g;
Example
2
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
1
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
1
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
add a comment |
Take a look from here http://forums.asp.net/t/1410702.aspx/1
Use this following Regular Expression Details, This will support leap year also.
var reg = /^(((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]|1d|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))))$/g;
Example
Take a look from here http://forums.asp.net/t/1410702.aspx/1
Use this following Regular Expression Details, This will support leap year also.
var reg = /^(((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]|1d|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))))$/g;
Example
edited Feb 11 '13 at 7:36
answered Feb 11 '13 at 7:29
OammieROammieR
1,84242245
1,84242245
2
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
1
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
1
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
add a comment |
2
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
1
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
1
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
2
2
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
This works like a charm. Tested a lot of dates and its works with leap years and also for months with 30 or 31 days.
– peter
Apr 27 '13 at 10:06
1
1
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
Unfortunately this fails for years earlier than 1900. For instance 01/01/1899 fails. Am I missing something here?
– Anjana Silva
Jun 23 '15 at 8:33
1
1
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
what if I want to check this format DD/MM/YYYY instead of MM/DD/YYYY?
– Ne AS
Mar 15 '17 at 10:51
add a comment |
Scape slashes is simply use before
/
and it will be escaped. (/
=> /
).
Otherwise you're regex DD/MM/YYYY could be next:
/^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/g
Explanation:
[0-9]
: Just Numbers
{2}
or{4}
: Length 2 or 4. You could do{2,4}
as well to length between two numbers (2 and 4 in this case)
[/]
: Character/
g
: Global -- Orm
: Multiline (Optional, see your requirements)
$
: Anchor to end of string. (Optional, see your requirements)
^
: Start of string. (Optional, see your requirements)
An example of use:
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
Of course you can use as boolean:
if(regex.test(date)){
//do something
}
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
add a comment |
Scape slashes is simply use before
/
and it will be escaped. (/
=> /
).
Otherwise you're regex DD/MM/YYYY could be next:
/^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/g
Explanation:
[0-9]
: Just Numbers
{2}
or{4}
: Length 2 or 4. You could do{2,4}
as well to length between two numbers (2 and 4 in this case)
[/]
: Character/
g
: Global -- Orm
: Multiline (Optional, see your requirements)
$
: Anchor to end of string. (Optional, see your requirements)
^
: Start of string. (Optional, see your requirements)
An example of use:
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
Of course you can use as boolean:
if(regex.test(date)){
//do something
}
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
add a comment |
Scape slashes is simply use before
/
and it will be escaped. (/
=> /
).
Otherwise you're regex DD/MM/YYYY could be next:
/^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/g
Explanation:
[0-9]
: Just Numbers
{2}
or{4}
: Length 2 or 4. You could do{2,4}
as well to length between two numbers (2 and 4 in this case)
[/]
: Character/
g
: Global -- Orm
: Multiline (Optional, see your requirements)
$
: Anchor to end of string. (Optional, see your requirements)
^
: Start of string. (Optional, see your requirements)
An example of use:
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
Of course you can use as boolean:
if(regex.test(date)){
//do something
}
Scape slashes is simply use before
/
and it will be escaped. (/
=> /
).
Otherwise you're regex DD/MM/YYYY could be next:
/^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/g
Explanation:
[0-9]
: Just Numbers
{2}
or{4}
: Length 2 or 4. You could do{2,4}
as well to length between two numbers (2 and 4 in this case)
[/]
: Character/
g
: Global -- Orm
: Multiline (Optional, see your requirements)
$
: Anchor to end of string. (Optional, see your requirements)
^
: Start of string. (Optional, see your requirements)
An example of use:
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
Of course you can use as boolean:
if(regex.test(date)){
//do something
}
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
var regex = /^[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/g;
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09",
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016",
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"];
//Iterate array
dates.forEach(
function(date){
console.log(date + " matches with regex?");
console.log(regex.test(date));
});
edited Nov 17 '18 at 2:26
Tim Higgins
129210
129210
answered Sep 20 '16 at 18:23
PauPau
5,53173565
5,53173565
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
add a comment |
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
your first test does not pass
– mibbit
Jan 9 '18 at 15:59
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
There are many valid dates that does not pass your regExp test
– AXL
Feb 1 '18 at 8:56
add a comment |
Try using this..
[0-9]{2}[/][0-9]{2}[/][0-9]{4}$
this should work with this pattern DD/DD/DDDD
where D is any digit (0-9)
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
add a comment |
Try using this..
[0-9]{2}[/][0-9]{2}[/][0-9]{4}$
this should work with this pattern DD/DD/DDDD
where D is any digit (0-9)
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
add a comment |
Try using this..
[0-9]{2}[/][0-9]{2}[/][0-9]{4}$
this should work with this pattern DD/DD/DDDD
where D is any digit (0-9)
Try using this..
[0-9]{2}[/][0-9]{2}[/][0-9]{4}$
this should work with this pattern DD/DD/DDDD
where D is any digit (0-9)
edited Aug 17 '15 at 14:02
MarmiK
4,57342841
4,57342841
answered Aug 17 '15 at 13:20
bkaancelenbkaancelen
5616
5616
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
add a comment |
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
Consider providing an explanation to your regexp
– arghtype
Aug 17 '15 at 13:41
add a comment |
If you are in Javascript already, couldn't you just use Date.Parse() to validate a date instead of using regEx.
RegEx for date is actually unwieldy and hard to get right especially with leap years and all.
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
add a comment |
If you are in Javascript already, couldn't you just use Date.Parse() to validate a date instead of using regEx.
RegEx for date is actually unwieldy and hard to get right especially with leap years and all.
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
add a comment |
If you are in Javascript already, couldn't you just use Date.Parse() to validate a date instead of using regEx.
RegEx for date is actually unwieldy and hard to get right especially with leap years and all.
If you are in Javascript already, couldn't you just use Date.Parse() to validate a date instead of using regEx.
RegEx for date is actually unwieldy and hard to get right especially with leap years and all.
answered Mar 28 '11 at 23:36
NaraenNaraen
2,76721518
2,76721518
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
add a comment |
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
I need regex because I'm using position-absolute.com/articles/…
– Juan Ignacio
Mar 30 '11 at 4:32
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Could you elaborate since that article is huge?
– jwize
Apr 21 '14 at 5:54
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
Date.parse doesn't seem to work with UK date format (dd/mm/yyyy) on Chrome. It only works with US format (mm/dd/yyyy).
– Jeff Shillitto
Mar 10 '15 at 1:52
add a comment |
((?=d{4})d{4}|(?=[a-zA-Z]{3})[a-zA-Z]{3}|d{2})((?=/)/|-)((?=[0-9]{2})[0-9]{2}|(?=[0-9]{1,2})[0-9]{1,2}|[a-zA-Z]{3})((?=/)/|-)((?=[0-9]{4})[0-9]{4}|(?=[0-9]{2})[0-9]{2}|[a-zA-Z]{3})
Regex Compile on it
2012/22/Jan
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012-Dec-22
2012-12-22
23/12/2012
23/12/2012
Dec-22-2012
12-2-2012
23-12-2012
23-12-2012
add a comment |
((?=d{4})d{4}|(?=[a-zA-Z]{3})[a-zA-Z]{3}|d{2})((?=/)/|-)((?=[0-9]{2})[0-9]{2}|(?=[0-9]{1,2})[0-9]{1,2}|[a-zA-Z]{3})((?=/)/|-)((?=[0-9]{4})[0-9]{4}|(?=[0-9]{2})[0-9]{2}|[a-zA-Z]{3})
Regex Compile on it
2012/22/Jan
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012-Dec-22
2012-12-22
23/12/2012
23/12/2012
Dec-22-2012
12-2-2012
23-12-2012
23-12-2012
add a comment |
((?=d{4})d{4}|(?=[a-zA-Z]{3})[a-zA-Z]{3}|d{2})((?=/)/|-)((?=[0-9]{2})[0-9]{2}|(?=[0-9]{1,2})[0-9]{1,2}|[a-zA-Z]{3})((?=/)/|-)((?=[0-9]{4})[0-9]{4}|(?=[0-9]{2})[0-9]{2}|[a-zA-Z]{3})
Regex Compile on it
2012/22/Jan
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012-Dec-22
2012-12-22
23/12/2012
23/12/2012
Dec-22-2012
12-2-2012
23-12-2012
23-12-2012
((?=d{4})d{4}|(?=[a-zA-Z]{3})[a-zA-Z]{3}|d{2})((?=/)/|-)((?=[0-9]{2})[0-9]{2}|(?=[0-9]{1,2})[0-9]{1,2}|[a-zA-Z]{3})((?=/)/|-)((?=[0-9]{4})[0-9]{4}|(?=[0-9]{2})[0-9]{2}|[a-zA-Z]{3})
Regex Compile on it
2012/22/Jan
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012/22/12
2012-Dec-22
2012-12-22
23/12/2012
23/12/2012
Dec-22-2012
12-2-2012
23-12-2012
23-12-2012
answered Jul 2 '15 at 6:06
user1693371user1693371
653
653
add a comment |
add a comment |
For people who needs to validate years earlier than year 1900, following should do the trick. Actually this is same as the above answer given by [@OammieR][1]
BUT with years including 1800 - 1899.
/^(((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((18|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((18|[2-9]d)d{2}))|((0[1-9]|1d|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))))$/
Hope this helps someone who needs to validate years earlier than 1900, such as 01/01/1855
, etc.
Thanks @OammieR
for the initial idea.
add a comment |
For people who needs to validate years earlier than year 1900, following should do the trick. Actually this is same as the above answer given by [@OammieR][1]
BUT with years including 1800 - 1899.
/^(((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((18|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((18|[2-9]d)d{2}))|((0[1-9]|1d|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))))$/
Hope this helps someone who needs to validate years earlier than 1900, such as 01/01/1855
, etc.
Thanks @OammieR
for the initial idea.
add a comment |
For people who needs to validate years earlier than year 1900, following should do the trick. Actually this is same as the above answer given by [@OammieR][1]
BUT with years including 1800 - 1899.
/^(((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((18|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((18|[2-9]d)d{2}))|((0[1-9]|1d|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))))$/
Hope this helps someone who needs to validate years earlier than 1900, such as 01/01/1855
, etc.
Thanks @OammieR
for the initial idea.
For people who needs to validate years earlier than year 1900, following should do the trick. Actually this is same as the above answer given by [@OammieR][1]
BUT with years including 1800 - 1899.
/^(((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((18|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((18|[2-9]d)d{2}))|((0[1-9]|1d|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))))$/
Hope this helps someone who needs to validate years earlier than 1900, such as 01/01/1855
, etc.
Thanks @OammieR
for the initial idea.
answered Jun 23 '15 at 14:37
Anjana SilvaAnjana Silva
2,32122332
2,32122332
add a comment |
add a comment |
Do the following change to the jquery.validationengine-en.js
file and update the dd/mm/yyyy inline validation by including leap year:
"date": {
// Check if date is valid by leap year
"func": function (field) {
//var pattern = new RegExp(/^(d{4})[/-.](0?[1-9]|1[012])[/-.](0?[1-9]|[12][0-9]|3[01])$/);
var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[/-.](0?[1-9]|1[012])[/-.](d{4})$/);
var match = pattern.exec(field.val());
if (match == null)
return false;
//var year = match[1];
//var month = match[2]*1;
//var day = match[3]*1;
var year = match[3];
var month = match[2]*1;
var day = match[1]*1;
var date = new Date(year, month - 1, day); // because months starts from 0.
return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day);
},
"alertText": "* Invalid date, must be in DD-MM-YYYY format"
add a comment |
Do the following change to the jquery.validationengine-en.js
file and update the dd/mm/yyyy inline validation by including leap year:
"date": {
// Check if date is valid by leap year
"func": function (field) {
//var pattern = new RegExp(/^(d{4})[/-.](0?[1-9]|1[012])[/-.](0?[1-9]|[12][0-9]|3[01])$/);
var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[/-.](0?[1-9]|1[012])[/-.](d{4})$/);
var match = pattern.exec(field.val());
if (match == null)
return false;
//var year = match[1];
//var month = match[2]*1;
//var day = match[3]*1;
var year = match[3];
var month = match[2]*1;
var day = match[1]*1;
var date = new Date(year, month - 1, day); // because months starts from 0.
return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day);
},
"alertText": "* Invalid date, must be in DD-MM-YYYY format"
add a comment |
Do the following change to the jquery.validationengine-en.js
file and update the dd/mm/yyyy inline validation by including leap year:
"date": {
// Check if date is valid by leap year
"func": function (field) {
//var pattern = new RegExp(/^(d{4})[/-.](0?[1-9]|1[012])[/-.](0?[1-9]|[12][0-9]|3[01])$/);
var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[/-.](0?[1-9]|1[012])[/-.](d{4})$/);
var match = pattern.exec(field.val());
if (match == null)
return false;
//var year = match[1];
//var month = match[2]*1;
//var day = match[3]*1;
var year = match[3];
var month = match[2]*1;
var day = match[1]*1;
var date = new Date(year, month - 1, day); // because months starts from 0.
return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day);
},
"alertText": "* Invalid date, must be in DD-MM-YYYY format"
Do the following change to the jquery.validationengine-en.js
file and update the dd/mm/yyyy inline validation by including leap year:
"date": {
// Check if date is valid by leap year
"func": function (field) {
//var pattern = new RegExp(/^(d{4})[/-.](0?[1-9]|1[012])[/-.](0?[1-9]|[12][0-9]|3[01])$/);
var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[/-.](0?[1-9]|1[012])[/-.](d{4})$/);
var match = pattern.exec(field.val());
if (match == null)
return false;
//var year = match[1];
//var month = match[2]*1;
//var day = match[3]*1;
var year = match[3];
var month = match[2]*1;
var day = match[1]*1;
var date = new Date(year, month - 1, day); // because months starts from 0.
return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day);
},
"alertText": "* Invalid date, must be in DD-MM-YYYY format"
edited May 25 '15 at 8:22
Daniel Olszewski
9,31623853
9,31623853
answered Mar 13 '13 at 19:23
Jayesh BhalodiaJayesh Bhalodia
1
1
add a comment |
add a comment |
I build this regular to check month 30/31 and let february to 29.
new RegExp(/^((0[1-9]|[12][0-9]|3[01])(/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(/)(02))|((0[1-9]|[12][0-9]|3[0])(/)(0[469]|11))(/)d{4}$/)
I think, it's more simple and more flexible and enough full.
Perhaps first part can be contract but I Don't find properly.
add a comment |
I build this regular to check month 30/31 and let february to 29.
new RegExp(/^((0[1-9]|[12][0-9]|3[01])(/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(/)(02))|((0[1-9]|[12][0-9]|3[0])(/)(0[469]|11))(/)d{4}$/)
I think, it's more simple and more flexible and enough full.
Perhaps first part can be contract but I Don't find properly.
add a comment |
I build this regular to check month 30/31 and let february to 29.
new RegExp(/^((0[1-9]|[12][0-9]|3[01])(/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(/)(02))|((0[1-9]|[12][0-9]|3[0])(/)(0[469]|11))(/)d{4}$/)
I think, it's more simple and more flexible and enough full.
Perhaps first part can be contract but I Don't find properly.
I build this regular to check month 30/31 and let february to 29.
new RegExp(/^((0[1-9]|[12][0-9]|3[01])(/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(/)(02))|((0[1-9]|[12][0-9]|3[0])(/)(0[469]|11))(/)d{4}$/)
I think, it's more simple and more flexible and enough full.
Perhaps first part can be contract but I Don't find properly.
answered Sep 4 '15 at 17:16
YannickIngenierieYannickIngenierie
961417
961417
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5465375%2fjavascript-date-regex-dd-mm-yyyy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Are you sure regex is the best option here?
– Alex
Mar 28 '11 at 21:39
1
Possible duplicate of Regex to validate date format dd/mm/yyyy
– rene
Sep 3 '16 at 19:34