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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it's working. I knew that regex could help but... damn that regex is hard to understand :P
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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.