Forum: Ruby on Rails Sanitising a Phone Number input

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7438e24e40d16589fd11a5b4b360c4d6?d=identicon&s=25 Michael Ward (michaelward82)
on 2006-04-24 21:52
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.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-24 22:17
(Received via mailing list)
Why not use

validates_format_of

http://lists.rubyonrails.org/pipermail/rails/2006-...

On Monday, April 24, 2006, at 9:52 PM, Michael Ward 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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
7438e24e40d16589fd11a5b4b360c4d6?d=identicon&s=25 Michael Ward (michaelward82)
on 2006-04-24 22:22
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 Olbrich wrote:
> Why not use
>
> validates_format_of
>
> http://lists.rubyonrails.org/pipermail/rails/2006-...
>
> On Monday, April 24, 2006, at 9:52 PM, Michael Ward 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
>>Rails@lists.rubyonrails.org
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> _Kevin
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-24 22:32
(Received via mailing list)
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 Ward wrote:
>> validates_format_of
>>>_______________________________________________
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-24 22:32
Michael Ward 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 Susser
http://blog.hasmanythrough.com
7438e24e40d16589fd11a5b4b360c4d6?d=identicon&s=25 Michael Ward (michaelward82)
on 2006-04-24 22:42
Thankyou, this was what I needed :)

Josh Susser wrote:
> Michael Ward 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 Susser
> http://blog.hasmanythrough.com
This topic is locked and can not be replied to.