ValidatesAsPhoneNumber
This Ruby on Rails plugin/gem implements an ActiveRecord validation helper called
validates_as_phone_number which validates U.S. phone numbers. The helper acts as
if validates_format_of was used with a regular expression that defines:
(000) 000 0000
(000) 000-0000
000.000.0000
000-000-0000
000-0000
000-000-0000x000 (extension can be up to 7 digits)
000-000-0000 x1234567 (extension can be up to 7 digits with or without leading space before 'x')
000-0000x000 (extension can be up to 7 digits)
Installation:
gem sources -a http://gems.github.com
Install the gem(s):
sudo gem install pivotal-validates_as_phone_number
Add to environment.rb initializer block:
config.gem 'pivotal-validates_as_phone_number', :version => '>=0.7.4', :lib => 'validates_as_phone_number', :source => 'http://gems.github.com'
Usage:
In your model file do something like:
class MyClass < ActiveRecord::Base
validates_as_phone_number :work_phone, :message => 'Invalid work phone number format', :allow_nil => true
end
Tests:
Some tests have been added.
License:
See the LICENSE file.