Forum: Ruby on Rails Regexp - date validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
90f2942bbc2d841f7e1459827467c67e?d=identicon&s=25 Brandon Kelly (brandonpt)
on 2006-06-05 15:06
Can somebody tell me why the following RegExp doesn't work for date
validation in Rails...   It validates fine in RadRails -- using REgExp
panel.  Intended format: (mm/dd/yyyy):


%r{^(0[1-9]|1[012])(-|/)(0[1-9]|[12][0-9]|3[01])(-|/)(19|20)([0-9][0-9])$}


If i try the following, it works in Rails:

%r{(0[1-9]|1[012])(-|/)(0[1-9]|[12][0-9]|3[01])}


When I add the last piece, it doesn't validate.

Any help would be appreciated.


- Brandon
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-06-05 17:39
(Received via mailing list)
On Jun 5, 2006, at 9:06 AM, Brandon Kelly wrote:

>
> %r{(0[1-9]|1[012])(-|/)(0[1-9]|[12][0-9]|3[01])}
>
>
> When I add the last piece, it doesn't validate.
>
> Any help would be appreciated.
>
>
> - Brandon
It worked for me.  I also changed the separators to simple character
classes and placed the year parts in non-capturing parentheses as
shown below.

%r{^(0[1-9]|1[012])[-/](0[1-9]|[12][0-9]|3[01])[-/]((?:19|20)(?:[0-9]
[0-9]))$}.match('12/31/1965').to_a
=> ["12/31/1965", "12", "31", "1965"]

What are you trying that "doesn't validate"?

-Rob
1a7418146bd26838d9280520e49f582d?d=identicon&s=25 Brandon Kelly (Guest)
on 2006-06-05 20:38
Rob Biedenharn wrote:
> It worked for me.  I also changed the separators to simple character
> classes and placed the year parts in non-capturing parentheses as
> shown below.
>
> %r{^(0[1-9]|1[012])[-/](0[1-9]|[12][0-9]|3[01])[-/]((?:19|20)(?:[0-9]
> [0-9]))$}.match('12/31/1965').to_a
> => ["12/31/1965", "12", "31", "1965"]
>
> What are you trying that "doesn't validate"?

Thanks for your reply, Rob.

I have an optional date_of_birth field --  if it's filled in, I'm trying
to validate the date format with that regexp.  The data I'm entering in
the date_of_birth text field is a simple date:

08/15/1970
11/13/1980

The expression I provided - when used in it's entirety, would fail
validation.  When I used only the first portion, it validated properly.
After adding the 2nd portion, it failed (even though the syntax is
correct).

I'll try your modified expression to see if that goes thru.  Is there
anything that would prevent a valid regexp from not processing correctly
-- that could be environment-oriented?  I use an regexp for my e-mail
validation that works fine though.

I'm running Windows XP, Ruby 1.8.2, and Rails.

Again - thanks for the help.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-06-05 21:14
(Received via mailing list)
On Jun 5, 2006, at 2:38 PM, Brandon Kelly wrote:

>
>
> validation that works fine though.
>
> I'm running Windows XP, Ruby 1.8.2, and Rails.
>
> Again - thanks for the help.
>
> --

Since your regexp is bracketed %r{^...$} any leading or trailing
whitespace (including a "\n") would cause your full expression to
fail.  Your partial-match is only anchored at the start %r{^...} so
any extra stuff is simply ignored.  Does that help?

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
1a7418146bd26838d9280520e49f582d?d=identicon&s=25 Brandon Kelly (Guest)
on 2006-06-05 21:24
> Since your regexp is bracketed %r{^...$} any leading or trailing
> whitespace (including a "\n") would cause your full expression to
> fail.  Your partial-match is only anchored at the start %r{^...} so
> any extra stuff is simply ignored.  Does that help?
>
> -Rob
>
> Rob Biedenharn		http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com

Yes --  thanks for your feedback.  I'll post the results later tonight.
Hopefully it works... otherwise, I'm bailing -- and just capturing the
year .. instead of the full date.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-06-06 11:25
(Received via mailing list)
Have a look at my validates_date_time plugin, it will validate dates
in the format you are after, as well as a bunch of others.

http://svn.viney.net.nz/things/rails/plugins/valid...

-Jonathan.
This topic is locked and can not be replied to.