zipcode validation doesn't work

5 views
Skip to first unread message

badnaam

unread,
Jul 25, 2010, 8:17:09 PM7/25/10
to Ruby on Rails: Talk
I am trying to do a very basic zipcode validation(zip is a int field
in mysql).

validates_presence_of :zip, :message => "Can't be blank"
validates_format_of :zip, :with => /^[0-9]{5}(-[0-9]{4})?$/, :message
=> "Invalid Zipcode"

No matter whatever valid 5 digit zip I enter, this does not work, but
If I do a
'12345'.match (/^[0-9]{5}(-[0-9]{4})?$/) it works just fine!


badnaam

unread,
Jul 25, 2010, 8:20:36 PM7/25/10
to Ruby on Rails: Talk
It seems both validations fail, the message says "Can't be blank and
shold be 5 digits", when its actually both.

Hassan Schroeder

unread,
Jul 25, 2010, 8:32:44 PM7/25/10
to rubyonra...@googlegroups.com
On Sun, Jul 25, 2010 at 5:17 PM, badnaam <asitk...@gmail.com> wrote:
> I am trying to do a very basic zipcode validation
** (zip is a int field in mysql) **

>  validates_format_of :zip, :with => /^[0-9]{5}(-[0-9]{4})?$/

Uh, you're trying to use a regular expression on an int? Off the top
of my head that seems unlikely to work...

> No matter whatever valid 5 digit zip I enter, this does not work, but
> If I do a
> '12345'.match (/^[0-9]{5}(-[0-9]{4})?$/) it works just fine!

.. as the above would seem to confirm.

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan

badnaam

unread,
Jul 25, 2010, 8:49:14 PM7/25/10
to Ruby on Rails: Talk
Thanks, I did change the field to varchar(5) as well, that doesn't
work eitehr

On Jul 25, 5:32 pm, Hassan Schroeder <hassan.schroe...@gmail.com>
wrote:
> On Sun, Jul 25, 2010 at 5:17 PM, badnaam <asitkmis...@gmail.com> wrote:
> > I am trying to do a very basic zipcode validation
>
> ** (zip is a int field in mysql) **
>
> >  validates_format_of :zip, :with => /^[0-9]{5}(-[0-9]{4})?$/
>
> Uh, you're trying to use a regular expression on an int? Off the top
> of my head that seems unlikely to work...
>
> > No matter whatever valid 5 digit zip I enter, this does not work, but
> > If I do a
> > '12345'.match (/^[0-9]{5}(-[0-9]{4})?$/) it works just fine!
>
> .. as the above would seem to confirm.
>
> HTH,
> --
> Hassan Schroeder ------------------------ hassan.schroe...@gmail.com
> twitter: @hassan

badnaam

unread,
Jul 25, 2010, 8:58:04 PM7/25/10
to Ruby on Rails: Talk
Nevermind, I did not put zip in attr_accessible, that's why it was
failing. Doh!!
Reply all
Reply to author
Forward
0 new messages