0

I want to validate the date format on an input using the format MMM DD, YYYY h:mm:ss A. i tried below, but didnt work:

var r = /^(((0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](0[1-9]|1\d|2[0-8])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](29)[\/\.-]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$/g;
r.test("Dec 08, 2015 3:11:00 AM");

i'm not really good at regex, any help would be appreciated!!

Thanks!

Amit
  • 45,440
  • 9
  • 78
  • 110
user1234
  • 3,000
  • 4
  • 50
  • 102

1 Answers1

-1

Regex isn't so great dealing with dates. For example, it's not suitable for differentiating between 30 or 31 days months. Your attempt further demonstrates how unreasonable it is, you've created an unreadable, unmaintainable expression and its virtually impossible to test or fix.

Instead, use a dedicated date/time component (either existing if it fits your requirements, or develop one yourself).

Amit
  • 45,440
  • 9
  • 78
  • 110