# Validates\_format\_of fails on empty field

**URL:** <https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555>\
**Category:** Rails\
**Created:** [December 28, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555 "2006-12-28T17:08:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 28, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555/1 "2006-12-28T17:08:29Z")

</div>

I have a phone number field with the following validation:

validates\_format\_of :phone, :with =\> /^\d\d\d-\d\d\d-\d\d\d\d$/,  
:message =\>  
“The phone number must be of the format: ###-###-####”

It works fine, when the phone number is known. But, sometimes the phone  
number is not known, which causes an error. Is there an elegant way to  
validate the format only if the field in not empty? I’d rather not  
write  
conditional logic into all the forms that use this.

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 28, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555/2 "2006-12-28T17:11:43Z")

</div>

Larry K. wrote:

> It works fine, when the phone number is known. But, sometimes the phone  
> number is not known, which causes an error. Is there an elegant way to  
> validate the format only if the field in not empty? I’d rather not  
> write  
> conditional logic into all the forms that use this.
> 
> –  
> Best Regards,  
> -Larry  
> “Work, work, work…there is no satisfactory alternative.”  
> — E.Taft Benson

validates\_presence\_of doesn’t catch it when it’s empty?

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 28, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555/3 "2006-12-28T17:22:32Z")

</div>

I’ve wondered about this too. Haven’t done anything with it though. It’s  
kind of hackish, but couldn’t you do something like  
with =\> /(^\d\d\d-\d\d\d-\d\d\d\d$|^$)/ to match the correct format OR  
an empty string?  
I’ve just confirmed that that does work. Maybe I’ll use that in my  
models. Thanks.

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 28, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555/4 "2006-12-28T17:34:36Z")

</div>

On Thu, 2006-12-28 at 08:07 -0800, Larry K. wrote:

> I have a phone number field with the following validation:
> 
> validates\_format\_of :phone, :with =\> /^\d\d\d-\d\d\d-\d\d\d\d  
> $/, :message =\> “The phone number must be of the format: ###-###-####”
> 
> It works fine, when the phone number is known. But, sometimes the  
> phone number is not known, which causes an error. Is there an elegant  
> way to validate the format only if the field in not empty? I’d rather  
> not write conditional logic into all the forms that use this.

* * *

most of the validations provide for :allow\_nil but I don’t see that  
listed in the API for validates\_format\_of

I do see listed :if =\>

which is probably what you are looking for

Craig

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 30, 2006, 12:08am UTC](https://www.ruby-forum.com/t/validates-format-of-fails-on-empty-field/80555/5 "2006-12-30T00:08:19Z")

</div>

Thanks for the responses, and the friendly help from this list. And  
thanks  
Jason for reminding me how incredibly powerful regular expressions are  
to  
those who know how to use them.

- Larry

On 12/28/06, Jason N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have a phone number field with the following validation:  
> > Best Regards,  
> > -Larry  
> > “Work, work, work…there is no satisfactory alternative.”  
> > — E.Taft Benson
> > 
> > >
> 
> >

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson
