Skip to content

Phone number input field to support international numbers, Angular

License

Notifications You must be signed in to change notification settings

cialfo/ngx-intl-tel-input

 
 

Repository files navigation

International Telephone Input for Angular (NgxIntlTelInput)

Changes done from after forking:

  1. Replace the use of google-libphonenumber with libphonenumber-js. (this library has smaller size).

    • projects/ngx-intl-tel-input/package.json
    • projects/ngx-intl-tel-input/src/lib/ngx-intl-tel-input.validator.ts
    • projects/ngx-intl-tel-input/src/lib/ngx-intl-tel-input.component.ts
  2. Previously, the phone validation was done by passing phoneValidation (boolean), now the validator has been exported separately and can be used as required

    • projects/ngx-intl-tel-input/src/public_api.ts
    • projects/ngx-intl-tel-input/src/lib/ngx-intl-tel-input.validator.ts
  3. The field got marked as dirty when the phone number was passed to it after it was initialized (e.g. after API response. The field will now be marked pristine if markPristineOnExternalChange is set to true.

  4. Country list got jumbled up as indicated in webcat12345#396. The fix was webcat12345#400

  5. It was upgraded to work with angular-12

    • angular.json
    • package.json
    • src/polyfills.ts
    • tsconfig.json
    • src/test.ts
    • src/environments/environment.ts
  6. In order create a dual field supporting email and mobile phone, we added a parameter allowedChars which can be passed to it so it can accept other characters as well instead of just ignoring them.

    • projects/ngx-intl-tel-input/src/lib/ngx-intl-tel-input.component.ts
  7. Added country code 246

npm version npm

An Angular package for entering and validating international telephone numbers. It adds a flag dropdown to any input, detects the user's country, displays a relevant placeholder and provides formatting/validation methods.

alt

Compatibility:

Validation with libphonenumber-js

ngx-intl-tel-input Angular ngx-bootstrap
3.x.x 9.x.x - 12.x.x 6.0.0
2.x.x 8.x.x - 9.1.x 5.6.x

Installation

Install Dependencies

$ npm install [email protected] --save

$ npm install libphonenumber-js --save

$ ng add ngx-bootstrap

If you do not wish to use Bootstrap's global CSS, we now package the project with only the relevant bootstrap styling needed for the dropdown. As such, you can remove the bootstrap styling from angular.json.

Further, Angular CLI should tree-shake the rest of Ngx-Boostrap away if you don't utilize other dependencies from the bootstrap package. This should keep this dependency a lean feature-add

Add Dependency Style

Add 'intl-tel-input' style file:

./node_modules/intl-tel-input/build/css/intlTelInput.css

to angular.json styles array:

"styles": [
  "./node_modules/intl-tel-input/build/css/intlTelInput.css",
  "src/styles.css"
],

Install This Library

$ npm install ngx-intl-tel-input --save

Usage

Import

Add NgxIntlTelInputModule to your module file:

imports: [NgxIntlTelInputModule];

Example

Refer to main app in this repository for working example.

<form #f="ngForm" [formGroup]="phoneForm">
	<ngx-intl-tel-input
		[cssClass]="'custom'"
		[preferredCountries]="[CountryISO.UnitedStates, CountryISO.UnitedKingdom]"
		[enableAutoCountrySelect]="false"
		[enablePlaceholder]="true"
		[searchCountryFlag]="true"
		[searchCountryField]="[SearchCountryField.Iso2, SearchCountryField.Name]"
		[selectFirstCountry]="false"
		[selectedCountryISO]="CountryISO.India"
		[maxLength]="15"
		[inputId]="my-input-id"
		name="phone"
		formControlName="phone"
	></ngx-intl-tel-input>
</form>

Options

Options Type Default Description
cssClass string control-form Bootstrap input css class or your own custom one.
preferredCountries <CountryISO>[] [] List of countries, which will appear at the top.
onlyCountries <CountryISO>[] [] List of manually selected countries, which will appear in the dropdown.
enableAutoCountrySelect boolean true Toggle automatic country (flag) selection based on user input.
enablePlaceholder boolean true Input placeholder text, which adapts to the country selected.
customPlaceholder string None Custom string to be inserted as a placeholder.
numberFormat <PhoneNumberFormat> PhoneNumberFormat.International Custom string to be inserted as a placeholder.
searchCountryFlag boolean false Enables input search box for countries in the flag dropdown.
searchCountryField <SearchCountryField>[] [SearchCountryField.All] Customize which fields to search in, if searchCountryFlag is enabled. Use SearchCountryField helper enum.
searchCountryPlaceholder string 'Search Country' Placeholder value for searchCountryField
maxLength number None Add character limit.
selectFirstCountry boolean true Selects first country from preferredCountries if is set. If not then uses main list.
inputId string phone Unique ID for <input> element.
selectedCountryISO <CountryISO> None Set specific country on load.
separateDialCode boolean false Visually separate dialcode into the drop down element.
countryChange <Country> None Emits country value when the user selects a country from the dropdown.

Supported Formats

Following formats are supported

  • NATIONAL // Produces "044 668 18 00"
  • INTERNATIONAL // Produces "+41 44 668 18 00"
  • E164 // Produces "+41446681800"

Helpful commands

  • Build lib: $ npm run build_lib
  • Copy license and readme files: $ npm run copy-files
  • Create package: $ npm run npm_pack
  • Build lib and create package: $ npm run package

Use locally

After building and creating package, you can use it locally too.

In your project run:

$ npm install --save {{path to your local '*.tgz' package file}}

Publishing to NPM

Before you can start using the package with the latest changes, you need to publish the package to NPM with a new version. To publish a new version of the package:

  • Go to projects/ngx-intl-tel-input and bump the version number in the package.json file.
  • Run npm run package in the root directory
  • Go to dist/ngx-splide
  • Login to Cialfo NPM account using npm adduser command (find credentials in vault)
  • Run npm publish --access public
  • Go the NPM page of the package https://www.npmjs.com/package/@cialfo/ngx-intl-tel-input to verify the latest version is published.
  • update the version of the ngx-intl-tel-input package in the package.json file of the project where the library is used and do an npm install.

About

Phone number input field to support international numbers, Angular

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 83.8%
  • HTML 6.7%
  • CSS 5.1%
  • JavaScript 4.4%