%r regex matching is ignoring my leading zeros

Discussion in 'Ruby' started by Dara Sanderson, Mar 29, 2006.

  1. Hello,

    I hope someone can help me or at least confirm that I'm not going
    insane.

    I have a model in which I'm trying to validate information before it
    saves.
    One particular field is for and expiration date. The code in my model
    to do the validation looks like this:

    validates_format_of :expiration,
    :with => %r{^[\d]{4,4}$},
    :message => "must be formatted (mmyy)"


    The problem is if someone enter a date that starts with a zero like
    "0309" The validation somehow just doesn't "see" the leading zero and
    keeps returning an error as though the user isn't submitting it
    properly. I did verify that the controller is definetly sending all
    four digits in the post.

    I tried different combinations like: r{^(0|1)[\d]{3}$} but no matter
    what I did the zero was ignored. I finally had to change it to look for
    3 or 4 digits just to make it work but that of course isn't proper
    validation as my submissions must have 4 digits.

    Can anyone help me? Am I going crazy here?

    Thanks,
    Dara

    --
    Posted via http://www.ruby-forum.com/.
     
    Dara Sanderson, Mar 29, 2006
    #1
    1. Advertising

  2. Dara Sanderson ha scritto:
    > Hello,
    >
    > I hope someone can help me or at least confirm that I'm not going
    > insane.
    >
    > I have a model in which I'm trying to validate information before it
    > saves.
    > One particular field is for and expiration date. The code in my model
    > to do the validation looks like this:
    >
    > validates_format_of :expiration,
    > :with => %r{^[\d]{4,4}$},
    > :message => "must be formatted (mmyy)"
    >
    >
    > The problem is if someone enter a date that starts with a zero like
    > "0309" The validation somehow just doesn't "see" the leading zero and
    > keeps returning an error as though the user isn't submitting it
    > properly. I did verify that the controller is definetly sending all
    > four digits in the post.


    I think I'd write the Regexp as:
    %r{^\d{4}$}

    Anyway, you one seem to work as expected on my box in irb:
    irb(main):001:0> x=/^[\d]{4,4}$/
    => /^[\d]{4,4}$/
    irb(main):002:0> x2=%r|^[\d]{4,4}$|
    => /^[\d]{4,4}$/
    irb(main):003:0> x.match "0102"
    => #<MatchData:0x2c0b300>
    irb(main):004:0> x2.match "0102"
    => #<MatchData:0x2c08fa8>

    and in rails:
    $cat app\models\foo.rb
    class Foo < ActiveRecord::Base
    validates_format_of :bar, :with=>%r{^\d{4}$}
    end

    >> f=Foo.new

    => #<Foo:0x39658e8 @new_record=true, @attributes={"bar"=>""}>
    >> f.bar ="0123"

    => "0123"
    >> f.save

    => true
    >> f.bar ="012"

    => "012"
    >> f.save

    => false

    what version of ruby are you running?
    Maybe it could have been some damn pesky problem like not having
    reloaded the model (this happened to me many times :) ?
     
    gabriele renzi, Mar 29, 2006
    #2
    1. Advertising

  3. Dara Sanderson

    Guest

    Hi --

    On Thu, 30 Mar 2006, Dara Sanderson wrote:

    > Hello,
    >
    > I hope someone can help me or at least confirm that I'm not going
    > insane.
    >
    > I have a model in which I'm trying to validate information before it
    > saves.
    > One particular field is for and expiration date. The code in my model
    > to do the validation looks like this:
    >
    > validates_format_of :expiration,
    > :with => %r{^[\d]{4,4}$},
    > :message => "must be formatted (mmyy)"
    >
    >
    > The problem is if someone enter a date that starts with a zero like
    > "0309" The validation somehow just doesn't "see" the leading zero and
    > keeps returning an error as though the user isn't submitting it
    > properly. I did verify that the controller is definetly sending all
    > four digits in the post.
    >
    > I tried different combinations like: r{^(0|1)[\d]{3}$} but no matter
    > what I did the zero was ignored. I finally had to change it to look for
    > 3 or 4 digits just to make it work but that of course isn't proper
    > validation as my submissions must have 4 digits.
    >
    > Can anyone help me? Am I going crazy here?


    My guess is that somewhere along the line the month is being turned
    into an integer and back to a string, so that "03" becomes 3 and then
    "3". I don't know where, though.


    David

    --
    David A. Black ()
    Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

    "Ruby for Rails" chapters now available
    from Manning Early Access Program! http://www.manning.com/books/black
     
    , Mar 29, 2006
    #3
  4. Dara Sanderson schrieb:
    > Hello,
    >
    > I hope someone can help me or at least confirm that I'm not going
    > insane.
    >
    > I have a model in which I'm trying to validate information before it
    > saves.
    > One particular field is for and expiration date. The code in my model
    > to do the validation looks like this:
    >
    > validates_format_of :expiration,
    > :with => %r{^[\d]{4,4}$},
    > :message => "must be formatted (mmyy)"
    >
    >
    > The problem is if someone enter a date that starts with a zero like
    > "0309" The validation somehow just doesn't "see" the leading zero and
    > keeps returning an error as though the user isn't submitting it
    > properly. I did verify that the controller is definetly sending all
    > four digits in the post.
    >
    > I tried different combinations like: r{^(0|1)[\d]{3}$} but no matter
    > what I did the zero was ignored. I finally had to change it to look for
    > 3 or 4 digits just to make it work but that of course isn't proper
    > validation as my submissions must have 4 digits.
    >
    > Can anyone help me? Am I going crazy here?
    >
    > Thanks,
    > Dara
    >


    i am not an expert in RE but don't you think [\d]{4,4} should be
    \d{4,4}

    inside a class [] . and i think \ lose their special meaning


    hth, Daniel
     
    Schüle Daniel, Mar 30, 2006
    #4
  5. First of all, thanks guys for trying to help.

    I changed my model validation to remove the brackets from around the \d.
    Still no dice.

    When I tried it irb as Gabriele did it worked fine though. Just doesn't
    want to work on the actual form I'm using.

    I'm afraid I dno't know how to test rails as you have here:
    >
    > and in rails:
    > $cat app\models\foo.rb
    > class Foo < ActiveRecord::Base
    > validates_format_of :bar, :with=>%r{^\d{4}$}
    > end
    >
    > >> f=Foo.new

    > => #<Foo:0x39658e8 @new_record=true, @attributes={"bar"=>""}>
    > >> f.bar ="0123"

    > => "0123"
    > >> f.save

    > => true
    > >> f.bar ="012"

    > => "012"
    > >> f.save

    > => false
    >
    > what version of ruby are you running?
    > Maybe it could have been some damn pesky problem like not having
    > reloaded the model (this happened to me many times :) ?


    I'm hosted on Dreamhost so they're on version 1.8.2 of ruby.

    Maybe it does have something to do with conversion to a string. Maybe
    I'll try and manipulate it in my controller before saving.

    Any other suggestions?


    --
    Posted via http://www.ruby-forum.com/.
     
    Dara Sanderson, Mar 30, 2006
    #5
  6. Dara Sanderson ha scritto:
    > First of all, thanks guys for trying to help.
    >
    > I changed my model validation to remove the brackets from around the \d.
    > Still no dice.
    >
    > When I tried it irb as Gabriele did it worked fine though. Just doesn't
    > want to work on the actual form I'm using.
    >
    > I'm afraid I dno't know how to test rails as you have here:


    this is just the the script/console thing in rails

    >>what version of ruby are you running?
    >>Maybe it could have been some damn pesky problem like not having
    >>reloaded the model (this happened to me many times :) ?

    >
    >
    > I'm hosted on Dreamhost so they're on version 1.8.2 of ruby.
    >
    > Maybe it does have something to do with conversion to a string. Maybe
    > I'll try and manipulate it in my controller before saving.
    >
    > Any other suggestions?


    It seem that the problem is hitting before the validation against the
    regex is actually called, the string is mangled somewhere.
    Maybe you can try defining your own validation method and checking that
    the value is the expected one, or if maybe it is truncated as david
    black suggested.
     
    gabriele renzi, Mar 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,020
    Oliver Sturm
    Oct 13, 2005
  2. Luke Zhang [MSFT]

    Re: CSV for Excel - Problem with Leading Zeros

    Luke Zhang [MSFT], Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,244
    Luke Zhang [MSFT]
    Jun 26, 2003
  3. GIMME
    Replies:
    5
    Views:
    43,731
    Dale King
    Feb 13, 2004
  4. r.magdeburg

    leading zeros - wrong int

    r.magdeburg, Dec 1, 2003, in forum: C++
    Replies:
    2
    Views:
    1,730
    red floyd
    Dec 1, 2003
  5. Stian
    Replies:
    2
    Views:
    606
    Thomas D'Tak
    Sep 19, 2004
Loading...

Share This Page