How to develop a pipe that formats phone numbers based on locale

0 votes
can someone help me with this by including relevant code or example, How to develop a pipe that formats phone numbers based on locale?
2 days ago in Angular by Nidhi
• 14,600 points
23 views

1 answer to this question.

0 votes

You can use the libphonenumber-js library for accurate international formatting.

Phone Number Formatting Pipe with libphonenumber-js

1. Install the Library

npm install libphonenumber-js

2. Create the Pipe

import { Pipe, PipeTransform } from '@angular/core';

import { parsePhoneNumberFromString } from 'libphonenumber-js';

@Pipe({

  name: 'phoneFormat'

})

export class PhoneFormatPipe implements PipeTransform {

  transform(phoneNumber: string, locale: string = 'US'): string {

    if (!phoneNumber) return '';

    const parsedNumber = parsePhoneNumberFromString(phoneNumber, locale.toUpperCase());

    return parsedNumber ? parsedNumber.formatInternational() : phoneNumber;

  }

}

3. Usage in Template

<p>{{ '+14155552671' | phoneFormat:'US' }}</p>

<!-- Output: +1 415 555 2671 -->

<p>{{ '919876543210' | phoneFormat:'IN' }}</p>

<!-- Output: +91 98765 43210 -->

answered 2 days ago by anonymous

Related Questions In Angular

0 votes
1 answer

How to develop a directive that modifies element styles based on form validation status?

Here's a concise directive that automatically applies ...READ MORE

answered 2 days ago in Angular by anonymous
18 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
6,320 views
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
106 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 views
0 votes
1 answer

How to implement a directive that manages focus on dynamically added elements?

To implement a directive that manages focus ...READ MORE

answered 2 days ago in Angular by anonymous
30 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP