问题描述
验证北美电话号码的规则是什么?另外,是否可以使用regex
?有没有宝石可以做到这一点?
What are the rules for validating a North American phone number? Also, is there a regex
I can use? Is there a gem to do this?
以下是我要牢记的规则
- 10位数字
- 没有特殊字符
- 一个正数
推荐答案
有很多宝石可以为您做到这一点.
There are many gems that will do this for you.
看看: http://rubygems. org/search?utf8 =%E2%9C%93& query =电话+号码
这个看起来像它会做您所需要的-它实质上实现了一个正则表达式来验证电话号码: http ://rubygems.org/gems/validates_phone_number
This one looks like it will do what you need -- it essentially implements a regex to validate the phone number: http://rubygems.org/gems/validates_phone_number
对于美国,加拿大(百慕大,巴哈马等,以及所有+1号码),正则表达式还应遵循其他规则.第一位(+1之后)必须为2-9.
For US, Canada (Bermuda, Bahamas... etc and all +1 numbers) there are other rules that the regex should follow. The first digit (after the +1) must be 2-9.
有关完整列表,请参见: http://en.wikipedia.org/wiki/North_American_Numbering_Plan
For a full list see: http://en.wikipedia.org/wiki/North_American_Numbering_Plan
这篇关于验证红宝石中的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!