Created
March 1, 2011 13:56
-
-
Save softwaregravy/849155 to your computer and use it in GitHub Desktop.
Accepting any 10 digit number in a string as a phone number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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