# Validates\_length\_of not working

**URL:** <https://www.ruby-forum.com/t/validates-length-of-not-working/87959>\
**Category:** Rails\
**Created:** [March 7, 2007, 5:01am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working/87959 "2007-03-07T05:01:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [March 7, 2007, 5:01am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working/87959/1 "2007-03-07T05:01:03Z")

</div>

I am attempting to validate a phone number field to assure that it is  
only 10 digits. I’ve placed the following code in my model:

validates\_length\_of :phone, :is =\> 10, :message =\> “should be 10  
digits.”, :allow\_nil =\> true

However, even when 10 digits are entered (with no dashes or parentheses)  
I still receive a validation error.

Any help would be appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [March 7, 2007, 9:15am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working/87959/2 "2007-03-07T09:15:58Z")

</div>

Kevin wrote:

> I am attempting to validate a phone number field to assure that it is  
> only 10 digits. I’ve placed the following code in my model:
> 
> validates\_length\_of :phone, :is =\> 10, :message =\> “should be 10  
> digits.”, :allow\_nil =\> true
> 
> However, even when 10 digits are entered (with no dashes or parentheses)  
> I still receive a validation error.
> 
> Any help would be appreciated. Thanks.

put this in your model…

def validate  
unless self.phone.to\_s.size == 10  
errors.add(“phone”, “should be 10 digits”)  
end  
end

URL =\> [Theocacao: Rails validates\_length\_of and Fixnum](http://theocacao.com/document.page/188)
