
Security News
Nx npm Packages Compromised in Supply Chain Attack Weaponizing AI CLI Tools
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
validates_and_formats_phones
Advanced tools
= ValidatesAndFormatsPhones
Allows you to easily format and validate phone numbers in your desired format (sensible defaults provided).
Just add 'validates_and_formats_phones' to your ActiveRecord model.
The default format is a 10-digit US phone number on the column 'phone'. But you can change all that...just read on.
== Installation
Install it as a gem:
(sudo) gem install validates_and_formats_phones
Then add it to your Gemfile file
gem 'validates_and_formats_phones', '~> 1.0.0'
Add the method validates_and_formats_phones to your model
== Example
Adding the validates_and_formats_phones method by itself gives you validation and formatting for the field :phone like so:
class Person validates_and_formats_phones end
person = Person.new(:phone => '123 456 7890') person.save person.phone => '(123) 456-7890'
person.phone = '123' person.save => false
You can pass your own fields like so:
class Person validates_and_formats_phones :fax, :mobile end
Or your own formats by using a # symbol for number substitutions:
class Person validates_and_formats_phones '####-####', '### ###-####' end
Note that the validation is simplistic. It counts the number of digits in the given field. If the number of digits in the given field matches one of the formats, then the validation passes and the format that has the same number of '#' symbols will be used to format the phone number.
You can also format and specify fields, like so:
class Person validates_and_formats_phones :fax, mobile, '####-####' end
== Configuration options
All configuration options available to validates_each are also available to the phone validator. For example:
class Person validates_and_formats_phones :fax, '###-###-####', :if => :some_function_name end
Here's a quick link to the validates_each documentation at the time of writing:
http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates_each
== Tiny string extension
As a tiny added benefit, String is extended with a to_phone method.
'123 456 7890'.to_phone => '(123) 456-7890'
'12345678'.to_phone('####-####') => '1234-5678'
=Change Log 1.0.0 No significant changes. 0.0.10
http://github.com/btelles-validates_and_formats_phones Copyright (c) 2010 [Bernie Telles], released under the MIT license
FAQs
Unknown package
We found that validates_and_formats_phones demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.