Skip to content

Instantly share code, notes, and snippets.

@softwaregravy
Created March 1, 2011 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softwaregravy/849155 to your computer and use it in GitHub Desktop.
Save softwaregravy/849155 to your computer and use it in GitHub Desktop.
Accepting any 10 digit number in a string as a phone number
class XXX < ActiveRecord::Base
before_validation :format_phone
validates_presence_of :phone
validates_length_of :phone, :is => 10
attr_accessible :phone
private
def format_phone
unless self.phone.nil?
formatted_phone = self.phone.scan(/\d+/).join
self.phone = formatted_phone
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment