1

I was wondering if somebody could point me to a regex code that validates for this: ####/##/##

Example: 1990/05/25

The second number 0 can only be 0 or 1 and the number 2 and only be 0, 1, 2, or 3. Other than that all other numbers in this set are allowed (0-9).

The code should validate that there is only 9 or 10 characters in total including the slashes.

Jerry
  • 70,495
  • 13
  • 100
  • 144
Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • 2
    Do you only want to validate the string or also validate the date itself? For instance, `2013/02/29` is not a valid date. Also, very important: what (programming) language are you using? – Jerry May 29 '13 at 13:13
  • Do you mean the character in position 0 can only be 0 or 1 and character in position 2 can only be 0,1,2 or 3? – Norman H May 29 '13 at 13:15
  • @Jerry Well yes I am basically validating that it is a date in this format 2013/05/29 If its possible I I would prefer if it also allows 2013/5/29 – Bagzli May 29 '13 at 13:15
  • @Norman The character with number 0 - Sorry should of been more specific – Bagzli May 29 '13 at 13:16

3 Answers3

3

If you only want to validate this format, you can use a regex like...

^\d{1,4}\/[01]?\d\/[0-3]\d$

I tested it a bit on some dates here.

This will match:

1990/01/01
2012/13/34
2013/1/39
9999/0/00

But reject:

23121/32/44
12/05/013
013/000/00

If you want to reject invalid dates as well such as 2013/02/29, you can check out this thread.

Community
  • 1
  • 1
Jerry
  • 70,495
  • 13
  • 100
  • 144
  • 12235/23/2/ passed the validation. So it can't serve my purpose :( – Bagzli May 29 '13 at 13:23
  • @Bagzli Did you include the anchors (`^` and `$`)? I added them a bit later. – Jerry May 29 '13 at 13:24
  • Thank you, I tried the new code and it seems to validate for all bad cases I can think of. I think I used your old code when I tried it the first time. – Bagzli May 29 '13 at 13:36
  • @Bagzli Yes, sorry about that, I wasn't thinking that you might have extra characters before and after the date initially. ^^; Glad that I was able to help! – Jerry May 29 '13 at 13:37
2

Try this (edit following Jerry)

[0-2][0-9]{3,3}/[0|1][0-9]/[0-3][0-9]

Mess about with the {a,b} notation to change the length of the general digits, it means between a and b of the preceding expression inclusive. It's unclear in your question where you want the digit flexibility to be.

E.g. to emit 2013/5/29, use

 [0-2][0-9]{3,3}/[0|1]{0,1}[0-9]/[0-3][0-9]
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I tried typing this in: 12235/23/2/ and it said it is valid. I really need it to check that the code is 9 or 10 digits long and to only allow specific numbers in certain spots as described in my question. – Bagzli May 29 '13 at 13:21
  • That fails on my regex parser (Boost C++) using either regex I've given. Which parser are you using? – Bathsheba May 29 '13 at 13:22
  • using this on asp.net (c#) ValidationSummary with CustomValidator – Bagzli May 29 '13 at 13:30
  • 1
    @Bathsheba This will not allow any year after 1999, like 2000 or 2013 because of the `[0|1]` at the start :(. Also, `[01]` is the same as `[0|1]`. – Jerry May 29 '13 at 13:31
  • I don't think your regex parser is doing what you want it to do. Why not test it with something simple like [0-9]? i.e. one digit. – Bathsheba May 29 '13 at 13:31
1

For all things regex I have found this website to be an invaluable resource. http://www.regular-expressions.info/reference.html

Specifically this page should get you what you need and contains a full explanation of how to go about validating date input format (not value) via Regular Expressions.

http://www.regular-expressions.info/dates.html

^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$

would match

yyyy-mm-dd
Norman H
  • 2,248
  • 24
  • 27