Validating phone number in ruby

前端 未结 6 720
渐次进展
渐次进展 2021-01-18 09:37

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?

Here are few rules I have

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 09:49

    There are many gems that will do this for you.

    Take a look at: http://rubygems.org/search?utf8=%E2%9C%93&query=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

    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.

    For a full list see: http://en.wikipedia.org/wiki/North_American_Numbering_Plan

提交回复
热议问题