-1

I'm trying to use javascript regex to validate a date input, but it is returning valid dates as invalid. I'm not an expert on regex so I don't know what is wrong with it:

/^([0-9]d{2})+(\.|-|\/)+([0-9]d{2})+(\.|-|\/)+([0-9]d{4})+$/

I want these date formats to be accepted:

23/04/2001

23-04-2001

23.04.2001

Originally I had this, but it was accepting dates with other characters on the end like 23/04/2001jhsdgf:

/\d{2}(\.|-|\/)\d{2}(\.|-|\/)\d{4}/;
Adam
  • 209
  • 3
  • 14

2 Answers2

4

Take your original regex and just add the ^ and $ from your new one to it. Problem solved.

EDIT: Although, really, the (\.|-|\/) is a mess and should be [.\/-] instead.

EDIT 2: That said, if you make the first one ([.\/-]) then you can replace the second one with \1 and then it would require both separators to be the same.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
4

You can check if a date is syntactically correct using a regex such as this one:

/^(\d{1,2})([-\.\/])(\d{1,2})\2(\d{4})$/
  • ^ and $ match beginning and end of string respectively
  • \d{1,2} matches 1 or 2 digits
  • \d{4} matches exactly 4 digits
  • \2 matches the string captured in second capturing group (- . /)

If matched, you can use the matched date, month and year to build a new date and compare the resulting date, month, year with the matched values. Here is a function which does just that:

function checkDate(dateText) {
    var match = dateText.match(/^(\d{1,2})([-\.\/])(\d{1,2})\2(\d{4})$/);
    // "31.04.2012" -> ["31.04.2012", "31", ".", "04", "2012"]
    if (match === null) {
        return false;
    }
    var date = new Date(+match[4], +match[3] - 1, +match[1]);
    return date.getFullYear() == +match[4] && 
      date.getMonth() == +match[3] - 1 && 
      date.getDate() == +match[1];
}
checkDate("30.04.2013"); // true
checkDate("31-04-2013"); // false (April has 30 days)
  • + is used to convert string to number (+"01" becomes 1)
  • Months are 0 based (0 = January, 1 = February, ...)
  • The above example assumes that the date is in dd-mm-yyyy format

The Date object attempts to correct invalid dates. Attempting to create a date such as 31-4-2013 yields 1-05-2013, the above function compares the resulting date with input parameters to check the validity.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Thanks, that has helped my understand things a whole lot better! For user friendliness and less need to validate, I have decided to use a JQuery UI datepicker and disable keypresses on that input. Seems to work a treat. Thanks for the help though, I appreciate it. – Adam Feb 04 '13 at 20:20
  • You're welcome. You can make the input `readonly`. – Salman A Feb 04 '13 at 20:21
  • For validate: mm/dd/yyyy
    
    function checkDate(dateText) {
    var match = dateText.match(/^(\d{1,2})([\/])(\d{1,2})\2(\d{4})$/);
            if (match === null) {
                return false;
            }
    
            var date = new Date(+match[4], +match[1] - 1, +match[3]);
            return date.getFullYear() === +match[4] && date.getMonth() === +match[1] - 1 && date.getDate() === +match[3];
    }
    
    
    – M.M.H.Masud Mar 15 '16 at 14:40