项目作者: niniverloer

项目描述 :
Get international phone number information
高级语言: JavaScript
项目地址: git://github.com/niniverloer/phonevalidator.git
创建时间: 2017-10-17T11:24:36Z
项目社区:https://github.com/niniverloer/phonevalidator

开源协议:

下载


PhoneValidator

Get international phone number informations

Getting Started

Phone Validator is a Javascript object that helps you to parse and validate a phone number written in an international format even if it contains other characters that are not numeric. It recognizes Country Code, Country Name, Country Continent, and other useful information. It’s also a full-featured object and have persistent information.

Built in

Phone Validator is built under Javascript without any dependency. It can be used wherever it is necessary to do a verification of the telephone number. It can also be used to get information about the current location of the user after checking the phone

Installing

CDN Integration

Version Spec
  1. <script type="text/javscript" src="https://unpkg.com/phonevalidator@1.1.4/phone.validator.js"></scirpt>
Latest Version
  1. <script type="text/javscript" src="https://unpkg.com/phonevalidator/phone.validator.min.js"></script>

Warning: When using the cdn link, you must change the default flag directory to use the country flag link

  1. <script>
  2. PhoneValidator.useDefaults({},{flagLink:"https://unpkg.com/phonevalidator@1.1.4/flags/$1.$2"});
  3. </script>

NPM Integration

Command
  1. npm install --save phonevalidator
Integrate
  1. <script type="text/javscript" src="./node_modules/phonevalidator/phone.validator.min.js"></script>
  2. <script type="text/javscript">
  3. PhoneValidator.useDefaults({},{flagLink:"./node_modules/phonevalidator/flags/$1.$2"});
  4. </script>

Local Repository

Download Phone Validator Git Repository
  1. git clone https://github.com/niniverloer/phonevalidator.git
Integrate
  1. <script type="text/javscript" src="./path_to_phonevalidator/phone.validator.min.js"></script>
  2. <script type="text/javscript">
  3. PhoneValidator.useDefaults({},{flagLink:"./path_to_phonevalidator/flags/$1.$2"});
  4. </script>

Basic Usage

Get the phone number from a string (even if it’s invalid)

  1. PhoneValidator.init("<string>").val()

i.e:

  1. PhoneValidator.init("ehfu00agufga23673j--00023848479dfaii").val()

Returns: +23673000238484 which is an invalid phone number

  1. PhoneValidator.init("(+380) 630-293-53-2").val()

Returns: +380630293532 which is a valid phone number

Note: PhoneValidator recognize 2 international characters 00 and +

Validity of a phone number

PhoneValidator deals with the phone format of all countries in the world to verify if it’s a valid phone number

  1. PhoneValidator.init("dsafngjaiodfjajdaoaa").isValid()

Returns: false

  1. PhoneValidator.init("ehfu00agufga23673j--00023848479dfaii").val()

Returns: +23673000238484

  1. PhoneValidator.init("+23673000238484").isValid()

Returns: false

  1. PhoneValidator.init("ehfu00agufga23673j--0002333dfaii").isValid()

Returns: true Because PhoneValidator.init("ehfu00agufga23673j--0002333dfaii").val() Returns +236730002333 which is a valid Central African Republic phone number format

  1. PhoneValidator.init(" +1 671-477-8355").isValid()

Returns : true

Get the phone country code

  1. PhoneValidator.init("<string>")["process"].countryCode

i.e:

  1. PhoneValidator.init(" +1 671-477-8355")["process"].countryCode

Returns: GU

Get the phone country name

  1. PhoneValidator.init("<string>")["process"].countryName

i.e:

  1. PhoneValidator.init(" +1 (671)-477-8355")["process"].countryName

Returns: Guam

Get the phone continent of the country

  1. PhoneValidator.init("<string>")["process"].countryContinent

i.e:

  1. PhoneValidator.init(" +1 671-477-8355")["process"].countryContinent

Returns: OCEANIA

  1. PhoneValidator.init("<string>")["process"].countryFlag

Make sure your country’s flag directory contains a correct value, See how to Integrate PhoneValidator in your current project in the previous topic

i.e:

  1. PhoneValidator.init(" +1 671-477-8355")["process"].countryFlag

Returns: https://unpkg.com/phonevalidator@1.1.2/flags/gu.svg
You can also change the country’s flag directory by this way

  1. PhoneValidator.init("<string>",{},{flagLink:"<flag_directory>"})["process"].countryFlag

i.e:

  1. PhoneValidator.init(" +1 671-477-8355",{},{flagLink:"./assets/js/phonevalidator/flags/$1.$2"})["process"].countryFlag

Returns: ./assets/js/phonevalidator/flags/gu.svg this method doesn't change default country's flag directory

Get the international country code in phone number format

  1. PhoneValidator.init("<string>")["process"].countryContinent

i.e:

  1. PhoneValidator.init("(+1 671)-477-8355")["process"].countryPhoneCode

Returns: 1671

Get the suscriber number

  1. PhoneValidator.init("<string>")["process"].suscriberNumber

i.e:

  1. PhoneValidator.init("+380-63-0293-532")["process"].suscriberNumber

Returns: 630293532

Get the valid phone number

  1. PhoneValidator.init("<string>")["process"].phoneNumber

i.e:

  1. PhoneValidator.init("+380-63-0293-532")["process"].phoneNumber

Returns: +380630293532

Working with Phone Validator Events

Phone Validator Events must be added before the initialization

When the string doesn’t contains a valid phone number

  1. PhoneValidator.addEventListener("invalid_number",function(number){
  2. // number can be a invalid phone number, null or an empty string
  3. if(number){
  4. console.log("Phone Number :"+number+" doesn't match any country phone format")
  5. }else{
  6. console.log("Please input a valid number")
  7. }
  8. });

Get the fixed number before getting extra information

  1. PhoneValidator.addEventListener("before_initialized",function(number){
  2. console.log("Fixed Number : ",number)
  3. });

When the phone number validation is finished

  1. PhoneValidator.addEventListener("all_got",function(process){
  2. console.log("Country Name : ",process.countryName)
  3. });

Author

David Niverloer