Created
December 8, 2011 10:30
-
-
Save chrisfinne/1446662 to your computer and use it in GitHub Desktop.
Is a US number SMSable?
This file contains hidden or 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 String | |
# http://en.wikipedia.org/wiki/North_American_Numbering_Plan | |
@@bad_phones = 0.upto(9).collect{|t| t.to_s*10} + ['231231234'] | |
def is_smsable? | |
return false unless self.is_phone? | |
return false if self.gsub(/[0123456789\-\.\+\(\) ]/,'').present? # non-phone number chars are present | |
return false if self =~ /\A\s*\+\s*[^1\s]/ # International number | |
us_ten = self.gsub(/\D/,'').gsub(/\A1/,'') | |
return false if us_ten.size != 10 | |
return false unless us_ten =~ /\A[2-9][0-8]\d[2-9]\d{6}\z/ | |
return false if us_ten =~/\A\d(00|11|22|33|44|55|66|77|88|99|00)/ # "ERCs" easily recognizable codes | |
return false if @@bad_phones.include?(us_ten) | |
return false if us_ten =~ /55501\d{2}\z/ or us_ten =~ /5551212\z/ or us_ten =~/\A555/ | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment