Forum: Ruby on Rails Regular Expression Question

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.
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 Andrew Filipowski (Guest)
on 2006-02-08 19:00
(Received via mailing list)
I have looked through the Ruby Pickaxe book as well as done a search
but can't figure out how to validate the format of a field. The field
I want to validate is a credit card expiration date field. Based on
our user study (going around the office and asking everyone the way
they like to fill out that particular field type) everyone prefers
the following format in one field:

MM/YY

They do not like to have to tab or to use dropdowns. I have tried
several iterations but I can't figure out the "/" I have tried:

%r{/(\d+)(\/)(\d+)/} (that is not a V it is \ /)

as well as:

/(\d+)(\/)(\d+)/

and neither work. Is it possible to look for a / in a regexp? If not
I guess I will break it out into two fields but would rather please
the majority of users than force them to do something because the
language doesn't allow me to do what I want to do.

Thanks

Andrew
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-02-08 19:14
(Received via mailing list)
Andrew Filipowski wrote:
> several iterations but I can't figure out the "/" I have tried:
>
> %r{/(\d+)(\/)(\d+)/} (that is not a V it is \ /)
>
> as well as:
>
> /(\d+)(\/)(\d+)/
>
> and neither work. Is it possible to look for a / in a regexp?

Hmm... this worked just fine for me:

[Pendelhaven] ~: irb
irb(main):001:0> a = "11/22"
=> "11/22"
irb(main):002:0> /(\d+)\/(\d+)/ === a
=> true
irb(main):003:0> quit


How are you trying to verify the regex against the field value?

Btw, unless you are planning to use the back-references that you've got
in your
pattern, which I doubt you are for a validation match, you can drop the
()s:

     /\d+\/\d+/ === a

Works just fine and doesn't waste memory creating back-references...

-Brian
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 Andrew Filipowski (Guest)
on 2006-02-08 19:22
(Received via mailing list)
Figured out my issue. had a typo in my code.

Andrew
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-08 20:57
(Received via mailing list)
Also bear in mind that this is going to match one or more digits
followed by a slash followed by one or more digits...
so it'll match 123123/2134523, etc. And even if you restrict the form
field to five chars, they can still enter 99/99.

b
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-08 21:46
(Received via mailing list)
I'm still pretty slow at regexes.... but I think this will do what you
want:

/^([0]?[1-9]|[1][0-2])\/[0-9]{2}$/

b
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-02-08 22:08
(Received via mailing list)
So if you KNOW that you're dealing with MM/YY, you can at least
use:    %r{(?:0[1-9]|1[012])/\d\d}

irb(main):004:0> a = %r{(?:0[1-9]|1[012])/\d\d}
=> /(?:0[1-9]|1[012])\/\d\d/
irb(main):005:0> a === "12/07"
=> true
irb(main):006:0> a === "92/08"
=> false
irb(main):007:0> a === "123/4"
=> false
irb(main):008:0> a === "13/06"
=> false

You could get tricky and include basic checks on the year, too:
%r{(?:0[1-9]|1[012])/(?:0[6-9]|1[0-5])}
If you assume an expiration will be between now and 2015.  (Of
course, you could get crazy with this and generate an RE that would
reject "01/06" since that's already passed, but if you're just
catching blatant typos, the first RE will be fine.

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