1.0.5 • Published 3 years ago

ion-intl-tel-input v1.0.5

Weekly downloads
187
License
See license in LI...
Repository
github
Last release
3 years ago

Interstellus Ionic International Telephone Input

An Ionic component for International Phone Number Input, that allows all countries, validation with google phone lib, limited countries, preferred countries, virtual scrolling and much more.

Contents

Supported Ionic Versions

  • Ionic 4 (>=4.0.0)

Getting Started

Step 1: Install it.

Install Independently

npm install ion-intl-tel-input --save

Or Install with All dependencies

npm install ion-intl-tel-input ionic-selectable flag-icon-css google-libphonenumber --save

Add flag styles

Add the following to your styles array of project in angular.json (located in projects root directory).

{
  "input": "node_modules/flag-icon-css/sass/flag-icon.scss"
}

Step 2: Import it.

First, import IsIonIntlTelInputModule to your app.module.ts that is normally located in src\app\app.module.ts.

import { IonIntlTelInputModule } from 'ion-intl-tel-input';

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

Note: Additionally, if you are using lazy loaded pages. Check if your pages have a module file, for example, home.module.ts, and if they do then import IsIonIntlTelInputModule to each page module too.

import { IonIntlTelInputModule } from 'ionic-selectable';
import { HomePage } from './home';

@NgModule({
  declarations: [
    HomePage
  ],
  imports: [
    IonicPageModule.forChild(HomePage),
    IonIntlTelInputModule
  ]
})
export class HomePageModule { }

Step 3: Add it to template.

a. Usage with Template Driven Forms

<form>
  <ion-item>
    <ion-label position="stacked">Tel Input</ion-label>
    <ion-intl-tel-input 
      id="phone-number" 
      name="phone-number" 
      [(ngModel)]="phoneNumber" 
      #phoneNumberControl="ngModel" >
    </ion-intl-tel-input>
  </ion-item>
</form>

b. Usage with Reactive Forms

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <ion-item>
    <ion-label position="floating">Tel Input</ion-label>
    <ion-intl-tel-input 
      formControlName="phoneNumber" >
    </ion-intl-tel-input>
  </ion-item>
</form>

Step 4: Configure it.

a. Usage with Template Driven Forms

@Component({ ... })
export class HomePage {

  phoneNumber = '';

  constructor() { }
}

b. Usage with Reactive Forms

@Component({ ... })
export class HomePage implements OnInit {

  formValue = {phoneNumber: '', test: ''};
  form: FormGroup;

  constructor() { }

  ngOnInit() {
    this.form = new FormGroup({
      phoneNumber: new FormControl({
        value: this.formValue.phoneNumber
      })
    });
  }

  get phoneNumber() { return this.form.get('phoneNumber'); }

  onSubmit() {
    console.log(this.phoneNumber.value);
  }
}

Step 5: Add validation.

a. Usage with Template Driven Forms

<form>
  <ion-item>
    <ion-label position="stacked">Tel Input</ion-label>
    <ion-intl-tel-input 
      id="phone-number" 
      name="phone-number" 
      [(ngModel)]="phoneNumber" 
      #phoneNumberControl="ngModel"
      ionIntlTelInputValid >
    </ion-intl-tel-input>
  </ion-item>

  <div *ngIf="phoneNumberControl.invalid && phoneNumberControl.touched">
    <ion-text color="danger" *ngIf="phoneNumberControl.errors.required">
      <p class="ion-no-margin"><sub>Phone is required.</sub></p>
    </ion-text>
    <ion-text color="danger" *ngIf="phoneNumberControl.errors.phone">
      <p class="ion-no-margin"><sub>Phone is not valid.</sub></p>
    </ion-text>
  </div>
</form>

b. Usage with Reactive Forms

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <ion-item>
    <ion-label position="floating">Tel Input</ion-label>
    <ion-intl-tel-input 
      formControlName="phoneNumber" >
    </ion-intl-tel-input>
  </ion-item>

  <div *ngIf="phoneNumber.invalid && phoneNumber.touched">
    <ion-text color="danger" *ngIf="phoneNumber.errors.required">
      <p class="ion-no-margin"><sub>Phone is required.</sub></p>
    </ion-text>
    <ion-text color="danger" *ngIf="phoneNumber.errors.phone">
      <p class="ion-no-margin"><sub>Phone number is not valid.</sub></p>
    </ion-text>
  </div>
</form>

Step 6: Configure validation.

a. Usage with Template Driven Forms

@Component({ ... })
export class HomePage {

  phoneNumber = '';

  constructor() { }
}

b. Usage with Reactive Forms

import { IonIntlTelInputValidators } from 'is-ion-intl-tel-input';

@Component({ ... })
export class HomePage implements OnInit {

  formValue = {phoneNumber: '', test: ''};
  form: FormGroup;

  constructor() { }

  ngOnInit() {
    this.form = new FormGroup({
      phoneNumber: new FormControl({
        value: this.formValue.phoneNumber
      }, [
        Validators.required,
        IonIntlTelInputValidators.phone
      ])
    });
  }

  get phoneNumber() { return this.form.get('phoneNumber'); }

  onSubmit() {
    console.log(this.phoneNumber.value);
  }
}

Options

OptionTypeDefaultDescription
defaultCountryisostringNULLIso Code of default selected Country.
dialCodePrefixstring+Determines whether to use 00 or + as dial code prefix. Available attributes are + and 00.
enableAutoCountrySelectbooleanfalseDetermines whether to select automatic country based on user input.
enablePlaceholderbooleantrueDetermines whether an example number will be shown as a placeholder in input.
fallbackPlaceholderstringNULLA fallaback placeholder to be used if no example number is found for a country.
inputPlaceholderstringNULLIf a custom placeholder is needed for input. If this property is set it will override enablePlaceholderand only this placeholder will be shown.
maxLengthstring15Maximum Length for input.
modalTitlestringSelect CountryTitle of modal opened to select country dial code.
modalCssClassstringNULLCSS class to attach to dial code selection modal.
modalSearchPlaceholderstringEnter country namePlaceholder for input in dial code selection modal.
modalCloseTextstringCloseText for close button in dial code selection modal.
modalCloseButtonSlotstringendSlot for close button in dial code selection modal. Ionic slots are supported.
modalCanSearchbooleantrueDetermines whether dial code selection modal should be searchable or not.
modalShouldBackdropClosebooleantrueDetermines whether dial code selection modal is closed on backdrop click.
modalShouldFocusSearchbarbooleantrueDetermines whether input should be focused when dial code selection modal is opened.
modalSearchFailTextstringNo countries foundDetermines whether input should be focused when dial code selection modal is opened.
onlyCountriesstring[][]List of iso codes of manually selected countries as string, which will appear in the dropdown. Note: onlyCountries should be a string array of country iso codes.
preferredCountriesstring[][]List of iso codes as string of countries, which will appear at the top in dial code selection modal. Note: preferredCountries should be a string array of country iso codes.
selectFirstCountrybooleantrueDetermines whether first country should be selected in dial code select or not.
separateDialCodebooleantrueDetermines whether to visually separate dialcode into the drop down element.

Events

Event NameTypeDescription
numberChangeEventEmitter<any>Fires when the Phone number Input is changed.
numberBlurEventEmitter<any>Fires when the Phone number Input is blurred.
numberFocusEventEmitter<any>Fires when the Phone number Input is focused.
numberInputEventEmitter<any>Fires when the user is typing in Phone number Input.
codeChangeEventEmitter<any>Fires when the dial code selection is changed.
codeOpenEventEmitter<any>Fires when the dial code selection modal is opened.
codeCloseEventEmitter<any>Fires when the dial code selection modal is closed.
codeSelectEventEmitter<any>Fires when a dial code is selected in dial code selection modal.

Contributing

This project follows the all-contributors specification. Contributions of any kind welcome!

Versioning

We use SemVer for versioning.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

This project would never have been possible without the following great plugins:

So please do go ahead and give them a star for their efforts.