Phone number format and validation library [closed]

北城余情 提交于 2019-11-26 16:08:40

问题


This question asking for a phone number format API in Java got me wondering why such an API doesn't seem to exist. Given the number of validation classes available under Apache Commons, I would have thought such an API would be included.

My question is, does anyone know of a phone number formatting/validation library available in any language that could be used as a model for a small Java open source project? Also, other than the large number of different phone number formats used throughout the world, does anyone know of any reason that such a project would be infeasible?


回答1:


Such an API in any language: Number::Phone (example submodule: Number::Phone::US).

Data: the ITU's National Numbering Plans index.




回答2:


Google recently released libphonenumber for parsing, formatting, storing and validating international phone numbers.




回答3:


At http://www.geonames.org/ they have a database you can download, theres regEx rules for phone numbers included in the country info dataset.

http://download.geonames.org/export/dump/

Look for the 'countryInfo.txt' file.

I suggest you build your own validator based upon that.




回答4:


Like it was said in the answers to the other question, no one's seen any for Java, and like you said, it would be nearly impossible due to the difference of phone number formats across the world.

And if you know the different formats for the regions that you're targeting, then it's usually pretty easy to implement it in regular expressions.




回答5:


You can check out Parsify Format here - https://www.mashape.com/parsify/format#endpoint-Phone-Number

It will return your number in several formats e.g.

{ "e164": "+1234567890", "national": "234567890", "international": "+1 234567890", "valid": false, "type": "UNKNOWN" }

You can get a more extensive response if it's a real number




回答6:


Phone Input field :

<input class="left" id="field" name="field" />

jquery Script:

jQuery.validator.addMethod("phoneUS", function(phone_number, element) { phone_number = phone_number.replace(/\s+/g, ""); return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(([2-9]\d{2})|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); }, "Please specify a valid phone number");



来源:https://stackoverflow.com/questions/501368/phone-number-format-and-validation-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!