1.0.23 • Published 5 months ago

angular-phone-number v1.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Angular Phone Number

A comprehensive international phone number input component for Angular applications with country selection, validation, and formatting.

Installation

To install the Angular Phone Number package, run the following command:

npm i angular-phone-number

This package implements country-specific validations using libphonenumber-js.

Install the required dependencies:

npm i libphonenumber-js -D

Flag Assets Setup

Important: To display country flags correctly, you need to copy the flag assets to your project:

  1. Copy the flag assets from the package to your project:

    cp -r node_modules/angular-phone-number/assets/flags src/assets
  2. Ensure your Angular project is configured to include the assets folder in your angular.json file:

    "assets": [
      "src/favicon.ico",
      "src/assets"
    ],

Usage

Import the AngularPhoneNumber module into your module file:

import { AngularPhoneNumber } from 'angular-phone-number';

Add AngularPhoneNumber to your module imports:

@NgModule({
  imports: [AngularPhoneNumber]
})
export class AppModule { }

This package displays country names in both Arabic and English.

Example

Here is an example of how to use the Angular Phone Number component in your application:

@Component({
  selector: 'app-root',
  template: `
    <form [formGroup]="myForm">
      <angular-phone-number
        formControlName="phoneNumber"
        [defaultCountry]="'LK'"
        [preferredCountries]="['LK', 'IN', 'GB']"
        [error]="myForm.get('phoneNumber')?.touched && myForm.get('phoneNumber')?.invalid"
        (countryChanged)="onCountryChanged($event)"
        (inputChanged)="onInputChanged($event)"
      ></angular-phone-number>
    </form>
  `,
  styles: []
})
export class AppComponent {
  myForm: FormGroup = new FormGroup({
    phoneNumber: new FormControl(''),
  });
  
  onCountryChanged(event: any) {
    console.log('Country changed:', event);
  }

  onInputChanged(event: any) {
    console.log('Input changed:', event);
  }
}

Options

The following options are available for the Angular Phone Number component:

OptionTypeDefaultDescription
defaultCountrystringnullSet the default country from the list.
preferredCountriesstring[]All countriesList of country codes to be displayed.
errorbooleanfalseDisplay the error status in the input box.
borderbooleantrueDisplay borders around the input box.
languagear enenChange country name based on language.
(countryChanged)EventEmitternullEmits an event when the country is changed.
(inputChanged)EventEmitternullEmits an event when the input is changed.

Country List

The component supports a comprehensive list of countries with their respective codes. Here are some examples:

  • US: United States
  • GB: United Kingdom
  • IN: India
  • LK: Sri Lanka
  • CA: Canada
  • AU: Australia

The full list includes over 200 countries and territories with their ISO codes.

1.0.19

5 months ago

1.0.18

5 months ago

1.0.22

5 months ago

1.0.21

5 months ago

1.0.20

5 months ago

1.0.23

5 months ago

1.0.17

5 months ago

1.0.16

5 months ago

1.0.15

5 months ago

1.0.14

6 months ago

1.0.13

6 months ago

1.0.12

6 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago