1

In my app all the dates are saved using java.util.Date format:

YYYY-mm-DD (year, month, day)

So 1988-05-13 is a valid date. So I think only digits are valid, month is [1:12], day is [1:30] E.g. this is a regular expression I use for my logins

/^[a-z]([0-9a-z_])+$/i

Which JavaScript regular expression should I use to check this date?

gabriel angelos
  • 349
  • 1
  • 9
  • 23
  • possible duplicate of [Regular expression for date format- dd-mm-yyyy in Javascript](http://stackoverflow.com/questions/8937408/regular-expression-for-date-format-dd-mm-yyyy-in-javascript) (Just change the order) – gdoron Mar 04 '13 at 15:53
  • Be sure the regular expression can say positive response for wrong date like 55551-99-23. So use try to use some pre-defined library to validate date. I feel "datejs" is more powerful handling this stuff. – HILARUDEEN S ALLAUDEEN Mar 04 '13 at 15:54
  • Ever thought about just using `split("-")`? – Andy E Mar 04 '13 at 15:55

2 Answers2

1

Let Date.parse() do the hard work for you:

function isValidDate(str){
    return !isNaN(Date.parse(str));
}

Date.parse(str) will return NaN if str isn't a valid date.

Zeta
  • 103,620
  • 13
  • 194
  • 236
0

Something like this:

/^\d\d\d\d-\d\d-\d\d$/

will check for the correct format - but it won't check for the valid range of the month and day. I don't think there is a way to do that with regular expressions.

MiMo
  • 11,793
  • 1
  • 33
  • 48