简体   繁体   中英

How to get the phone local number from international phone number with IoNIC

I'm using IONIC4 in my project, I managed to get all the countries with its own CallingCodes and country code, Now I'm getting stuck to get the local phone format(a mask to display in the placeholder) for the selected country.

I give an example, suppose that the selected country is France, its country code is: FR and its calling code is (33) so how I get the following mask:

07 45 85 96 32

Is there a way to do that using IONIC please ?

You could install libphonenumber: npm install --save-prod google-libphonenumber

Then, you can use the AsYouTypeFormatter to mask the number as it's being typed.

// Require `AsYouTypeFormatter`.
const AsYouTypeFormatter = require('google-libphonenumber').AsYouTypeFormatter;
const formatter = new AsYouTypeFormatter('US');

console.log(formatter.inputDigit('2')); // => 2
console.log(formatter.inputDigit('0')); // => 20
console.log(formatter.inputDigit('2')); // => 202
console.log(formatter.inputDigit('-')); // => 202-
console.log(formatter.inputDigit('4')); // => 202-4
console.log(formatter.inputDigit('5')); // => 202-45
console.log(formatter.inputDigit('6')); // => 202-456
console.log(formatter.inputDigit('-')); // => 202-456-
console.log(formatter.inputDigit('1')); // => 202-456-1
console.log(formatter.inputDigit('4')); // => 202-456-14
console.log(formatter.inputDigit('1')); // => 202-456-141
console.log(formatter.inputDigit('4')); // => 202-456-1414

// Cleanup all input digits from instance.
formatter.clear();

To get the format in France, simply pass 'FR' to the formatter

const formatter = new AsYouTypeFormatter('FR');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM