# Validation Problem

**URL:** <https://www.ruby-forum.com/t/validation-problem/156635>\
**Category:** Rails\
**Created:** [December 31, 2008, 6:56am UTC](https://www.ruby-forum.com/t/validation-problem/156635 "2008-12-31T06:56:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [December 31, 2008, 6:56am UTC](https://www.ruby-forum.com/t/validation-problem/156635/1 "2008-12-31T06:56:45Z")

</div>

Hello-

I’m trying to do something pretty basic - validate a form field  
against a regex. Here’s the line of my model that adds the validation:

validates\_format\_of :zipcode, :with =\> /\d{5}(-\d{4})?/, :message =\>  
‘is failing the regex’

When I enter the value “12345” into the form field, it comes back and  
tells me that “Zipcode is failing the regex”. What’s odd is that I’ve  
gotten sporadic success, where the validations will pass - sometimes  
with values that failed previously.

I feel confident of the regex itself and testing it in IRB yields the  
results I would expect. Once I use it in the validation, then it stops  
working.

Does anyone know what might be going on? Failing that, any ideas for  
where to start troubleshooting this would be appreciated.

Regards,  
Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [December 31, 2008, 7:07am UTC](https://www.ruby-forum.com/t/validation-problem/156635/2 "2008-12-31T07:07:53Z")

</div>

Ah, I just figured it out!

As an aside, and I’m not sure if anyone else notices the same thing,  
but it seems like the easiest way for me to solve my own problem is to  
ask for help. Never fails.

The issue, in case someone else runs into it, was that I had  
accidentally created a new method on my class called “zipcode” (it  
returned a ZipCode object, where the zipcode is a string). Somehow,  
perhaps based on whether or not zipcode was set, the validation was  
using the string sometimes and the object the rest of the time (yes,  
yes, I know strings are objects too). This is why my results were  
erratic.

Regards,  
Eric

On Dec 31, 12:56 am, emarthinsen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
