# Sanitising a Phone Number input

**URL:** <https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689>\
**Category:** Rails\
**Created:** [April 24, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689 "2006-04-24T21:52:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 24, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689/1 "2006-04-24T21:52:01Z")

</div>

I want to sanitise a phone number input.

In my UsersController.rb file I have written a method that will take a  
string and reduce it to numbers only.

This works find if I call it manually, but in the interests of keeping  
DRY I would like to ensure that the code sanitisation method is called  
whenever the data is edited (I’m using standard scaffolidng for this  
test).

Is there anyway I can do this and keep the code overhead low?

My current sanitisation method looks like this:

private  
def numericalise!(\*number)  
for n in number.phone do  
n.gsub!(/[^0-9]/, ‘’)  
end  
end

Thanks in advance for your time and help.

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 24, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689/2 "2006-04-24T22:17:06Z")

</div>

Why not use

validates\_format\_of

[http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html](http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html)

On Monday, April 24, 2006, at 9:52 PM, Michael W. wrote:

> Is there anyway I can do this and keep the code overhead low?
> 
> Thanks in advance for your time and help.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 24, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689/3 "2006-04-24T22:22:16Z")

</div>

I want to remove thr formatting, not check for it. It’s to be expected  
that user may put spaces, commas, periods and parenthisis in a phone  
number and I don’t want to force them in to a particular format.

If I sanitise the code first (then validate it for completeness) then  
I’m happy with the data and the user finds the input easy.

Kevin O. wrote:

> Why not use
> 
> validates\_format\_of
> 
> [http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html](http://lists.rubyonrails.org/pipermail/rails/2006-February/015659.html)
> 
> On Monday, April 24, 2006, at 9:52 PM, Michael W. wrote:
> 
> > Is there anyway I can do this and keep the code overhead low?
> > 
> > Thanks in advance for your time and help.
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> \_Kevin

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 24, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689/4 "2006-04-24T22:32:41Z")

</div>

Then just call your sanitizer on the attribute in question in the update  
or create methods of your controller.

On Monday, April 24, 2006, at 10:22 PM, Michael W. wrote:

> > validates\_format\_of
> > 
> > > * * *
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 24, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689/5 "2006-04-24T22:32:42Z")

</div>

Michael W. wrote:

> I want to remove thr formatting, not check for it. It’s to be expected  
> that user may put spaces, commas, periods and parenthisis in a phone  
> number and I don’t want to force them in to a particular format.
> 
> If I sanitise the code first (then validate it for completeness) then  
> I’m happy with the data and the user finds the input easy.

You could use the before\_validation callback to clean the data before it  
gets validated.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 24, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/sanitising-a-phone-number-input/58689/6 "2006-04-24T22:42:10Z")

</div>

Thankyou, this was what I needed 🙂

Josh S. wrote:

> Michael W. wrote:
> 
> > I want to remove thr formatting, not check for it. It’s to be expected  
> > that user may put spaces, commas, periods and parenthisis in a phone  
> > number and I don’t want to force them in to a particular format.
> > 
> > If I sanitise the code first (then validate it for completeness) then  
> > I’m happy with the data and the user finds the input easy.
> 
> You could use the before\_validation callback to clean the data before it  
> gets validated.
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
