Sanitising a Phone Number input


#1

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.


#2

Why not use

validates_format_of

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/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#3

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

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/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#4

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

_Kevin


#5

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


#6

Thankyou, this was what I needed :slight_smile:

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