35

I am trying to validate datetime format MM/DD/YYYY. Here is the code I am trying please help.

 function ValidateDate(testdate) {
        var Status
        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]|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))))$/g;
        if (!reg.test(testdate)) {
            Status = false;
        }
        return Status;
    }
Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Supreet
  • 2,451
  • 8
  • 25
  • 42
  • 2
    Refer to http://www.devnetwork.net/viewtopic.php?f=29&t=13795 – Deadlock Mar 04 '13 at 07:24
  • 1
    What exactly is wrong with your code? – nhahtdh Mar 04 '13 at 07:28
  • something is wrong with regular expression – Supreet Mar 04 '13 at 07:50
  • 3
    Trying to validate a date with a regular expression is a good way to go quickly insane - getting it structured to accept all the days of each month, but not invalid days of each month causes a combinatorial explosion. Think about it this way ... you want to reject the 32nd of any month, the 31st for several months, including 06/31 of any year, you have to accept 02/28 every year but reject 02/29 most years, though you should accept it if the year is divisible by 4 but not if by 100, though if the year is divisible by 400 then 02/29 is ok. – Bevan Mar 04 '13 at 08:18

7 Answers7

91

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);
}
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
noirbizarre
  • 3,429
  • 1
  • 30
  • 21
14

The answer marked is perfect but for one scenario, where in the dd and mm are actually single digits. the following regex is perfect in this case:

    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);
    }
Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
3

based on this

dd-mm-yy

I modified the original to this:

^(?:(?:(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec))(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:(?:0?2|(?:Feb))(\/|-|\.)(?:29)\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

test the regex here

Community
  • 1
  • 1
luisnicg
  • 304
  • 2
  • 3
0
var pattern = new RegExp((0|1)[0-9]\/[0-3][0-9]\/(19|20)[0-9]{2});
if(!testdate.match(pattern))
   return false;
else return true;
MIIB
  • 1,849
  • 10
  • 21
0

you can look into this link and I hope this may be of great help as its was for me.

If you are using the regex given in the above link in Java code then use the following regex

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

As I noticed in your example you are using js function then this use regex

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

This will match a date in mm/dd/yyyy format from between 1900-01-01 and 2099-12-31, with a choice of four separators. and not to forget the

return regex.test(testdate);

kavinder
  • 609
  • 1
  • 5
  • 13
0

In this case, to validate Date (DD-MM-YYYY) or (DD/MM/YYYY), with a year between 1900 and 2099,like this with month and Days validation

if (!Regex.Match(txtDob.Text, @"^(0[1-9]|1[0-9]|2[0-9]|3[0,1])([/+-])(0[1-9]|1[0-2])([/+-])(19|20)[0-9]{2}$").Success)                  
{
   MessageBox.Show("InValid Date of Birth");
   txtDob.Focus();
}
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
-3

It's long, but great:

new RegExp(/(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/);
Gustavo Rossi Muller
  • 1,062
  • 14
  • 18
  • While code often speaks for itself, answers that only contain code and have no textual explanation, get flagged for the review queues. You can avoid this by adding a sentence or two of explanation. – Will Jul 07 '16 at 03:32
  • Did not explant what function it included and make people confuse. – Tony Dong Sep 26 '16 at 22:53