Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete dial code #359

Open
Alexise857 opened this issue Feb 14, 2021 · 5 comments
Open

Delete dial code #359

Alexise857 opened this issue Feb 14, 2021 · 5 comments

Comments

@Alexise857
Copy link

Alexise857 commented Feb 14, 2021

Hi all, so basically I can succesfully save to my backend the phone number with its dial code, which is used to send sms, however, once I get the phone number back from my server, it shows the flag and the number, I would like to only have the number without the dial code since showing the flag is enought

https://stackblitz.com/edit/ngx-intl-tel-input-demo-ng-10-fybvpx?file=src/app/app.component.ts

@pasevin
Copy link
Collaborator

pasevin commented Mar 9, 2021

I think you should do the manipulation yourself. Meaning, remove the dial code manually. Unless you have a better proposal :)

@Alexise857
Copy link
Author

Any idea on how can that be done?

@pasevin
Copy link
Collaborator

pasevin commented Mar 9, 2021

If your phone numbers are saved in a consistent format using "internationalNumber": "+1 415-629-7292", field. Then I think it can be cleaned like so:

private removeDialCode(phoneNumber: string): string {
    if (phoneNumber.startsWith('+')) {
        phoneNumber = phoneNumber.substr(phoneNumber.indexOf(' ') + 1);
    }
    return phoneNumber;
}

@Alexise857
Copy link
Author

That would work if I had the following formar +1 415-629-7292; however I'm storing on my database +14156297292 without any space, in that case, do you know how to find the dial code?

@gotwig
Copy link

gotwig commented Aug 30, 2022

If you use a formControl I think this is the code you are looking for:

this.formControl.valueChanges.subscribe((event: any) => {

  if (this.formControl) {
    if (event?.number?.includes('+')) {
      this.formControl.setValue(this.formControl.value.nationalNumber);
    }

}

This code does the following: when formControl value gets set first, it will trigger this function. Then it checks if the current shown number includes a + (so includes dial code), if it does, we set the current value to the nationalNumber instead, which is the one without dial code.

I hope this solves the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants