webcat12345 / ngx-intl-tel-input

Phone number input field to support international numbers, Angular
MIT License
213 stars 335 forks source link

how can I convert e164Number value to ChangeData object? #402

Open fwitkowski opened 2 years ago

fwitkowski commented 2 years ago

In my project I am using ngx-intl-tel-input for international phone numbers. When user sets value it has this format (ChangeData interface):

{
        number: "212-555-1234",
        internationalNumber: "+1 212-555-1234",
        nationalNumber: "(212) 555-1234",
        e164Number: "+12125551234",
        countryCode: "US",
        dialCode: "+1"
}

I want to save to database only e164Number, which is no issue. The issue is when I want to convert it back to ChangeData object.

The plugin does not provide public function to do this; at least I cannot find one.

I see that original library (JS): intl-tel-input has a public function setNumber, see in package documentation, however I cannot find a way to user that directly in Angular.

vickyRathee commented 2 years ago

@fwitkowski Did you find any workaround for this? I also need to save e164Number only as string in my database

tampo80 commented 2 years ago

try this

import * as lpn from 'google-libphonenumber';

const PNF = lpn.PhoneNumberFormat;

     /*  const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();*/

//e164NumberFromDb is the e164Number from your database const phoneNumber = this.phoneUtil.parseAndKeepRawInput(e164NumberFromDb);

      const phone= {
        "number":phoneNumber.getNationalNumber(),
        "internationalNumber": this.phoneUtil.format(phoneNumber, PNF.INTERNATIONAL),
        "nationalNumber": this.phoneUtil.format(phoneNumber, PNF.NATIONAL),
        "e164Number": this.phoneUtil.format(phoneNumber, PNF.E164),
        "countryCode": this.phoneUtil.getRegionCodeForNumber(phoneNumber),
        "dialCode": '+' +phoneNumber.getCountryCode()
      }
      console.log(phone);