Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vandrijevik-has_phone_number

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vandrijevik-has_phone_number

  • 1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

HasPhoneNumber

HasPhoneNumber makes phone number validations in ActiveRecord easy. Make sure your model has a string attribute to store your phone number, pass it to has_phone_number, and you're done!

class Person < ActiveRecord::Base
  has_phone_number :cell_phone
end

This will ensure that the phone number in cell_phone is of a valid phone number format, and invalidate Person if the phone number is invalid. It will also play nice with form_for, so you can display the errors to your users.

Using Formats

By default, has_phone_number will validate against a US phone number format. For this, all you need to do is:

class Person < ActiveRecord::Base
  has_phone_number :cell_phone
end

If, however you want to validate against a different phone number format, you can:

class Office < ActiveRecord::Base
  has_phone_number :main, :format => :us
  has_phone_number :abroad, :format => :france
end

This will validate the main attribute against a US number format (included), and abroad against a France number format (if you've specified it).

Specifying Formats

Format names and specifications are stored in the HasPhoneNumber::PhoneNumber.formats hash. The key is a symbol (that can be used with the :format option to has_phone_number), and the value is a regex that the attribute is matched against.

In a Rails project, you can specify additional formats a number of ways, but the cleanest is to put your number formats in an intializer. For example, config/initializers/phone_number_formats.rb:

my_formats = {:my_number_format => /\d{10}/,
              :no_area_code => /\d{3}-\d{4}/}

HasPhoneNumber::PhoneNumber.formats.update(my_formats)

Hope you like it!

Copyright (c) 2009 [Vladimir Andrijevik], released under the MIT license

FAQs

Package last updated on 10 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc