# Validates\_numericality\_of, :allow\_nil =\> true

**URL:** <https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/49418>\
**Category:** Rails\
**Created:** [December 4, 2005, 2:18am UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/49418 "2005-12-04T02:18:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [December 4, 2005, 2:18am UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/49418/1 "2005-12-04T02:18:35Z")

</div>

I’m trying to use validates\_numericality\_of on a form of mine, using  
:allow\_nil =\> true, but when I submit the form with nothing filled in I  
still get the “is not a number” error.

Help?

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [December 4, 2005, 2:42am UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/49418/2 "2005-12-04T02:42:54Z")

</div>

FYI: I’m using this right now…

validates\_format\_of :account\_number, :with =\> /([0-9]\*)/

…but it’s just annoying me that validates\_numericality\_of isn’t  
working as  
advertised.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [December 4, 2005, 3:39am UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/49418/3 "2005-12-04T03:39:39Z")

</div>

Sorry for all the spam…this works better

/^[\d]\*$/

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [December 4, 2005, 1:39pm UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/49418/4 "2005-12-04T13:39:15Z")

</div>

subimage interactive \<subimage@…\> writes:

> I’m trying to use validates\_numericality\_of on a form of mine, using  
> :allow\_nil =\> true, but when I submit the form with nothing filled in I  
> still  
> get the “is not a number” error.Help?

When you don’t fill in a field on a form, the value that gets passed to  
your  
controller is NOT nil–it’s an empty string, “” (omitting the quotes, of  
course). While it may seem pedantic, an empty string is still a string,  
not a nil.

To test numericality and still allow the field to be empty you could do  
something like this:  
validates\_numericality\_of :some\_field,  
:if =\> Proc.new {|c| not  
c.some\_field.blank?}

–Forrest
