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
Andrew Filipowski wrote:> 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?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
Figured out my issue. had a typo in my code. Andrew On Feb 8, 2006, at 1:13 PM, Brian V. Hughes wrote:> Andrew Filipowski wrote: >> 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? > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 Brian V. Hughes wrote:> Andrew Filipowski wrote: > >> 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? > > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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
Ben Munat wrote:> 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
>
> Brian V. Hughes wrote:
>
>> Andrew Filipowski wrote:
>>
>>> 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?
>>
>>
>>
>> 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
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
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
At 2/8/2006 02:56 PM, you wrote:>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
>
>Brian V. Hughes wrote:
>>Andrew Filipowski wrote:
>>
>>>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?
>>
>>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