No commit activity in last 3 years
No release in over 3 years
Adds validation methods to ActiveModel for validating phone numbers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

= 2.10.0
= 3.1.0
 Project Readme

Build Status

Validates Phone Number

validates_phone_number adds validation methods to ActiveModel for validating phone numbers.

Basic Usage

Given a User model with a phone number attribute of phone number that's a string, see if a given record has a valid phone number like so:

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => true
end

By default, both 10 and 7 digit numbers are valid.

Options

Seven Digits

Example: 555-5555, 555.5555, 1234567, etc.

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:seven_digits => true}
end

Ten Digits

Example: (555)-555-5555, 555.555.5555, 1234567890, etc.

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:ten_digits => true}
end

Message

Set the message that's added to the errors collection by using the message option:

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:message => "invalid and can only be attributable to human error"}
end

Format/Match by Regular Expression

You can specify that a number matches a regular expression by using the format option:

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:format => /\d{3}-\d{3}-\d{4}/}
end

Allow Nil

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:allow_nil => true}
end

Allow Blank

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:allow_blank => true}
end

Multiple

class Person < ActiveRecord::Base
  validates :mobile, :phone_number => {:ten_digits => true, :seven_digits => true, :allow_blank => true, :message => "Phone number must be either seven or digits in length, or blank."}
end

Self-Promotion

Like validates_phone_number? Follow the project on GitHub.

You should follow travisjeffery on GitHub and Twitter too. Thanks!