# Zipcode validation doesn't work

**URL:** <https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191>\
**Category:** Rails\
**Created:** [July 26, 2010, 2:18am UTC](https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191 "2010-07-26T02:18:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [July 26, 2010, 2:18am UTC](https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191/1 "2010-07-26T02:18:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [July 26, 2010, 2:21am UTC](https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191/2 "2010-07-26T02:21:41Z")

</div>

It seems both validations fail, the message says “Can’t be blank and  
shold be 5 digits”, when its actually both.

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [July 26, 2010, 2:33am UTC](https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191/3 "2010-07-26T02:33:58Z")

</div>

On Sun, Jul 25, 2010 at 5:17 PM, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [July 26, 2010, 2:50am UTC](https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191/4 "2010-07-26T02:50:22Z")

</div>

Thanks, I did change the field to varchar(5) as well, that doesn’t  
work eitehr

On Jul 25, 5:32 pm, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [July 26, 2010, 2:59am UTC](https://www.ruby-forum.com/t/zipcode-validation-doesnt-work/192191/5 "2010-07-26T02:59:09Z")

</div>

Nevermind, I did not put zip in attr\_accessible, that’s why it was  
failing. Doh!!
