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

Message

Specify 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