A powerful Angular directive for phone number validation and formatting using the Google libphonenumber library. This package provides a seamless way to handle phone number inputs in your Angular applications with built-in validation, formatting, and accessibility features.
- 🔍 Smart Validation: Robust validation using Google's libphonenumber library
- 🌍 Country Code Detection: Automatic detection and formatting of country codes
- 📱 Formatting Options: Format to international or national format
- 🔄 Two-Way Binding: Seamless integration with Angular's template-driven forms
- ♿ Accessibility: ARIA labels and keyboard navigation support
- 🎨 Dark Mode: Built-in support for dark mode themes
- 🎯 Error Handling: Clear validation errors and feedback
- 🔧 Customizable: Easy to integrate and customize
View the live demo to see the component in action.
- Angular 17+ project
- TailwindCSS 3 configured in your project
- Angular forms
npm install @ngx-tailwind-ui/phonenumbers
Add the TauiPhonenumbersDirective
to your component imports. If you're using template-driven forms, also import FormsModule
:
import { FormsModule } from '@angular/forms';
import { TauiPhonenumbersDirective } from '@ngx-tailwind-ui/phonenumbers';
@Component({
// ...
imports: [FormsModule, TauiPhonenumbersDirective],
})
Add the directive to your input field:
<input type="tel" phonenumber defaultCountryCode="+1" [countryCodeControl]="countryCodeControl" [(ngModel)]="phoneNumber" />
Input | Type | Required | Description |
---|---|---|---|
defaultCountryCode |
string | No | Sets the default country code for the input (e.g., "+1" for US). Can include or omit the '+' prefix. |
countryCodeControl |
AbstractControl | No | Binds the country code to a form control for two-way binding support. |
The directive automatically adds the following validators:
-
required
: Ensures the input is not empty -
phoneNumber
: Validates the phone number format using libphonenumber
The directive provides built-in error messages for:
- Required field validation: "Phone number is required"
- Invalid phone number format: "Please enter a valid phone number"
Simple phone number input with default country code:
<input type="tel" phonenumber defaultCountryCode="+1" [(ngModel)]="phoneNumber" />
Split input for country code and phone number:
<div class="flex gap-2">
<!-- Country code input -->
<input type="text" [(ngModel)]="countryCode" readonly class="w-20" />
<!-- Phone number input -->
<input type="tel" phonenumber defaultCountryCode="+57" [countryCodeControl]="countryCodeControl" [(ngModel)]="phoneNumber" />
</div>
Complete form example with validation and error messages:
<form #phoneForm="ngForm">
<input type="tel" name="phone" phonenumber defaultCountryCode="+57" [(ngModel)]="phoneNumber" required #phone="ngModel" />
<div *ngIf="phone.invalid && (phone.dirty || phone.touched)">
<div *ngIf="phone.errors?.['required']">Phone number is required</div>
<div *ngIf="phone.errors?.['phoneNumber']">Please enter a valid phone number</div>
</div>
<button type="submit" [disabled]="!phoneForm.valid">Submit</button>
</form>
The package is tested and supported on the following browsers:
- Chrome (latest)
- Safari (latest)
- Edge (latest)
-
Peer Dependencies:
- @angular/common: ^19.2.0
- @angular/core: ^19.2.0
- @angular/forms: ^19.2.0
- awesome-phonenumber: ^7.2.0
-
Runtime Dependencies:
- tslib: ^2.3.0
- Change Detection: Optimized to minimize change detection cycles
- Validation: Efficient phone number validation
- Bundle Size: Minimal impact on bundle size
- Memory Usage: Efficient memory management
The component includes built-in accessibility features:
- ARIA labels for screen readers
- Keyboard navigation support
- Focus management
- Mobile-friendly input
Contributions are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.