0

I would like to check if the date format is (dd/mm/yyyy) before the post is made in a JavaScript (jQuery).

How could I achieve that?

ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
Warface
  • 4,919
  • 8
  • 52
  • 82

2 Answers2

10

You can use a regex:

/^\d{2}\/\d{2}\/\d{4}$/.test(str)

Of course this will not check for a valid date, but in that case you can always let your server-side code throw an error. If you want more client-side validation have a look at Javascript: how to validate dates in format MM-DD-YYYY?; it is easy to adapt to your format.

Community
  • 1
  • 1
ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
  • Thanks it's working. I knew that regex could help but... damn that regex is hard to understand :P – Warface May 17 '12 at 15:16
  • 2
    Oops, just fixed it - looks like nobody who upvoted it noticed that I forgot to escape the slashes. The regex is pretty easy btw: `^$` mark the start/end of the string, `\d` is a digit, `{2}` means that you want exactly two occurrences. – ThiefMaster May 17 '12 at 15:22
1

i think that would be very difficult, considering the ambiguity for cases where the date is less than equal to 12

Sachin Nayak
  • 1,031
  • 9
  • 17