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 -- Posted via http://www.ruby-forum.com/.
On Jun 5, 2006, at 9:06 AM, Brandon Kelly wrote:> 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. > > > - BrandonIt 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
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. -- Posted via http://www.ruby-forum.com/.
On Jun 5, 2006, at 2:38 PM, Brandon Kelly wrote:> 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. > > --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
> 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.comYes -- 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. -- Posted via http://www.ruby-forum.com/.
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/validates_date_time/ -Jonathan. On 6/6/06, Brandon Kelly <bkelly01@gmail.com> wrote:> > 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >