# How to remove spaces from phone number

**URL:** <https://www.ruby-forum.com/t/how-to-remove-spaces-from-phone-number/86575>\
**Category:** Rails\
**Created:** [February 23, 2007, 11:39am UTC](https://www.ruby-forum.com/t/how-to-remove-spaces-from-phone-number/86575 "2007-02-23T11:39:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [February 23, 2007, 11:39am UTC](https://www.ruby-forum.com/t/how-to-remove-spaces-from-phone-number/86575/1 "2007-02-23T11:39:15Z")

</div>

Hi,

I need to allow login based on the phone number. During the signup they  
can give the number in any format with spaces, slash or (). But for  
storing in database and then during login to compare I just want the 10  
digit number. How can I remove the special char from phone number before  
storing to db ?

Thanks.

---

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [February 23, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-remove-spaces-from-phone-number/86575/2 "2007-02-23T12:00:22Z")

</div>

Rm Rm wrote:

> I need to allow login based on the phone number. During the signup they  
> can give the number in any format with spaces, slash or (). But for  
> storing in database and then during login to compare I just want the 10  
> digit number. How can I remove the special char from phone number before  
> storing to db ?

before\_save {|o| o.phone\_number.gsub!(/\D/, “”)

or something like that. I don’t recall if before\_save accepts a block  
like that, though. If it doesn’t just create a named method instead and  
call that from before\_filter.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [March 11, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/how-to-remove-spaces-from-phone-number/86575/3 "2007-03-11T21:24:01Z")

</div>

Jakob S. wrote:

> Rm Rm wrote:
> 
> > I need to allow login based on the phone number. During the signup they  
> > can give the number in any format with spaces, slash or (). But for  
> > storing in database and then during login to compare I just want the 10  
> > digit number. How can I remove the special char from phone number before  
> > storing to db ?
> 
> before\_save {|o| o.phone\_number.gsub!(/\D/, “”)
> 
> or something like that. I don’t recall if before\_save accepts a block  
> like that, though. If it doesn’t just create a named method instead and  
> call that from before\_filter.
> 
> –  
> Jakob S. - [http://mentalized.net](http://mentalized.net)

Here is how I solved this problem.

In your model, put the following:

class Client \< ActiveRecord::Base

validates\_numericality\_of :phone\_number  
validates\_length\_of :phone\_number, :is =\> 10

before\_validation :normalize\_phone\_number # this gets called  
first

def normalize\_phone\_number  
self.phone\_number.gsub!( /[\s()-]/, ‘’ ) # cleans out the junk  
end  
end

Now to get this to display nicely, I used the helper number\_to\_phone.

\<%= number\_to\_phone @client.phone\_number, :area\_code =\> true %\>

If this is in a form and needs be edited, I do something like this:

\<%= start\_form\_tag :action =\> ‘update’, :id =\> @client%\>

Phone Number

\<%= text\_field ‘client’, ‘phone\_number’, :value =\> number\_to\_phone(  
@client.phone\_number, :area\_code =\> true ), :size=\> 20 %\>

…  
\<%= submit\_tag ‘Save’ %\>  
\<%= end\_form\_tag %\>

Note: I used the older start\_form\_tag convention, but I think it should  
also work with the newer form\_for convention as well.

Hope this helps.

Mike
