All Projects → mobi → Telephone_number

mobi / Telephone_number

Licence: mit
Phone number validation for Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Telephone number

React Phone Number Input
React component for international phone number input
Stars: ✭ 725 (+176.72%)
Mutual labels:  phone, number
Vue Phone Number Input
A phone number input made with Vue JS (format & valid phone number)
Stars: ✭ 407 (+55.34%)
Mutual labels:  phone, number
React Phone Input 2
📞 Highly customizable phone input component with auto formatting
Stars: ✭ 446 (+70.23%)
Mutual labels:  phone, number
Libphonenumber Js
A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
Stars: ✭ 2,233 (+752.29%)
Mutual labels:  phone, number
Brazilian Utils
Utils library for specific Brazilian businesses
Stars: ✭ 1,023 (+290.46%)
Mutual labels:  validator, phone
yup-phone
☎️ Adds a phone number validation check to yup validator using google-libphonenumber
Stars: ✭ 219 (-16.41%)
Mutual labels:  phone, number
phonenumber
With a given country and phone number, validate and format the MOBILE phone number to E.164 standard
Stars: ✭ 108 (-58.78%)
Mutual labels:  phone, number
PhoneNumberKit
Android Kotlin library to parse and format international phone numbers. Country code picker.
Stars: ✭ 124 (-52.67%)
Mutual labels:  phone, number
number-to-words
convert number into words (english, french, italian, roman, spanish, portuguese, belgium, dutch, swedish, polish, russian, iranian, roman, aegean)
Stars: ✭ 53 (-79.77%)
Mutual labels:  number
cti-pattern-validator
OASIS TC Open Repository: Validate patterns used to express cyber observable content in STIX Indicators
Stars: ✭ 18 (-93.13%)
Mutual labels:  validator
checker
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.
Stars: ✭ 62 (-76.34%)
Mutual labels:  validator
ThinCreditCard
💳 Simple way to add a credit card
Stars: ✭ 40 (-84.73%)
Mutual labels:  validator
laravel-authy
Rinvex Authy is a simple wrapper for @authy TOTP API, the best rated Two-Factor Authentication service for consumers, simplest 2fa Rest API for developers and a strong authentication platform for the enterprise.
Stars: ✭ 35 (-86.64%)
Mutual labels:  phone
thai-citizen-id-validator
🦉 Validate Thai Citizen ID with 0 dependencies 🇹🇭
Stars: ✭ 35 (-86.64%)
Mutual labels:  validator
mcloud
Mobile farm ecosystem for Android and iOS devices
Stars: ✭ 32 (-87.79%)
Mutual labels:  phone
FireFiles
Powerful Android File Manager for everything that runs on Android OS (Android TV, Android Watch, Mobile, etc)
Stars: ✭ 37 (-85.88%)
Mutual labels:  phone
twilio-voice
A Google Voice-like service you can run yourself with Twilio
Stars: ✭ 68 (-74.05%)
Mutual labels:  phone
spec-prod
GitHub Action to build ReSpec/Bikeshed specs, validate output and publish to GitHub pages or W3C
Stars: ✭ 19 (-92.75%)
Mutual labels:  validator
jsonlint
Lightweight command-line tool for validating JSON
Stars: ✭ 27 (-89.69%)
Mutual labels:  validator
MuditaOS
Mobile operating system based on FreeRTOS™ optimized for E Ink displays - developed for Mudita Pure minimalist phone
Stars: ✭ 349 (+33.21%)
Mutual labels:  phone

Code Climate Coverage Status

What is it?

TelephoneNumber is global phone number validation gem based on Google's libphonenumber library.

Demo

Feel free to check out our demo!

Numberjack

Installation

Add this line to your application's Gemfile:

gem 'telephone_number'

And then execute:

$ bundle

Or install it yourself as:

$ gem install telephone_number

Supported Ruby Versions

This library requires Ruby 2.4 or later.

Rails Validation

validates :my_attribute_name, telephone_number: {country: proc{|record| record.country}, types: [:fixed_line, :mobile, etc]}

Valid Phone Types

  • :area_code_optional
  • :fixed_line
  • :mobile
  • :no_international_dialling
  • :pager
  • :personal_number
  • :premium_rate
  • :shared_cost
  • :toll_free
  • :uan
  • :voicemail
  • :voip

Country

  • In this example, record.country must yield a valid two letter country code such as :us, :ca or 'DE'
  • You can also just pass a String or Symbol instead of a Proc.

Manual Usage

You can obtain a TelephoneNumber object by calling:

phone_object = TelephoneNumber.parse("3175082237", :us) ==>

#<TelephoneNumber::Number:0x007fe3bc146cf0
  @country=:US,
  @e164_number="13175083348",
  @national_number="3175083348",
  @original_number="3175083348">    

After that you have the following instance methods available to you.

  • valid_types

    Returns all types that the number is considered valid for.

    phone_object.valid_types ==> [:fixed_line, :mobile, :toll_free]

  • valid?

    Returns boolean value indicating whether or not valid_types is empty.

    phone_object.valid? ==> true

  • national_number(formatted: true)

    Returns the national formatted number including special characters such as parenthesis and dashes. You can omit the special characters by passing formatted: false

    phone_object.national_number ==> "(317) 508-2237"

  • international_number(formatted: true)

    Returns the international formatted number including special characters such as parenthesis and dashes. You can omit the special characters by passing formatted: false

    phone_object.international_number ==> "+1 317-508-2237"

  • e164_number(formatted: true)

    Returns the international formatted number including special characters such as parenthesis and dashes. You can omit the special characters by passing formatted: false

    phone_object.e164_number ==> "+13175082237"

  • country

    Returns an object containing data related a the number's country.

    phone_object.country ===>
     #<TelephoneNumber::Country:0x007fb976267410
     @country_code="1",
     @country_id="US",
     ...
    
  • location

    Returns the location of the number. Default locale is :en

    phone_object.location ==> "Indiana"

    phone_object.location(:ja) ==> "ソウル特別市"

  • timezone

    Returns the time zone of the number.

    phone_object.timezone ==> "America/New_York"

Class Methods

You also have the following class methods available to you.

  • parse

    Returns a TelephoneNumber object.

    TelephoneNumber.parse("3175082237", :US)

    If you pass an E164 formatted number, we will determine the country on the fly.

    TelephoneNumber.parse("+13175082237")

  • valid?

    Returns boolean value indicating whether or not a particular number is valid.

    TelephoneNumber.valid?("3175082237", :US) ==> true

    If you are looking to validate against a specific set of keys, you can pass in an array of keys

    TelephoneNumber.valid?("3175082237", :US, [:mobile, :fixed_line]) ==> true
    TelephoneNumber.valid?("3175082237", :US, [:toll_free]) ==> false
    
  • invalid?

    Returns boolean value indicating whether or not a particular number is invalid.

    TelephoneNumber.invalid?("3175082237", :US) ==> false

    If you are looking to invalidate against a specific set of keys, you can pass in an array of keys

    TelephoneNumber.invalid?("3175082237", :US, [:mobile, :fixed_line]) ==> false
    TelephoneNumber.invalid?("3175082237", :US, [:toll_free]) ==> true
    

Configuration

Override File

In the event that you need to override the data that Google is providing, you can do so by setting an override file. This file is expected to be in the same format as Google's as well as serialized using Marshal.

To generate a serialized override file:

ruby bin/console
TelephoneNumber.generate_override_file("/path/to/file")

In this instance, /path/to/file represents an xml file that has your custom data in the same structure that Google's data is in.

You can set the override file with:

TelephoneNumber.override_file = "/path/to_file.dat"

Default Number Pattern

If TelephoneNumber is passed an invalid number and then asked to format that number, it will simply return an unformatted string of the originally passed number. This is because formatting rules will not be found for invalid numbers. If this is unacceptable, you can set a default_format_pattern and default_format_string that TelephoneNumber will use attempt to format invalid numbers.

TelephoneNumber.default_format_pattern = "(\\d{3})(\\d{3})(\\d*)"
TelephoneNumber.default_format_string = "($1) $2-$3"

invalid_number = "1111111111"
phone_object = TelephoneNumber.parse(invalid_number, :US)
phone_object.national_number ==> "(111) 111-1111"

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

While developing new functionality, you may want to test against specific phone numbers. In order to do this, add the number to lib/telephone_number/test_data_generator.rb and then run rake data:test:import. This command will reach out to the demo application provided by Google and pull the correct formats to test against.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mobi/telephone_number. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].