百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

telephone_number

> 编程语言
开源

Ruby 电话号码验证

396 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Ruby 电话号码验证

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:

ruby
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) ==>

#

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.

…
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
…

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](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

Issues· 13 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubynumberphonerubyvalidator

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言