UNPKG

@mdfrough/cnic-validator

Version:

An Angular library to validate and format Pakistani CNIC numbers with masking and pipe support.

160 lines (113 loc) โ€ข 3.21 kB
# CNIC Validator for Angular A lightweight and powerful Angular library for validating, formatting, and parsing Pakistani CNIC numbers. Includes input masking, validation, formatting pipe and directive. ## ๐Ÿ“ฆ Features - โœ… CNIC input validation (13-digit format: `xxxxx-xxxxxxx-x`) - โœ… CNIC input masking directive (auto-inserts hyphens) - โœ… CNIC validator directive for template-driven forms - โœ… Validation errors via Angular Reactive Forms - โœ… CNIC formatting pipe - โœ… Built for Angular 17+ - โœ… Supports both NgModules and Standalone Components --- ## ๐Ÿš€ Installation ```bash npm i @mdfrough/cnic-validator ``` --- ## ๐Ÿ”ง Setup ### Option 1: Using NgModules ```ts import { CnicValidatorModule } from '@mdfrough/cnic-validator'; @NgModule({ imports: [CnicValidatorModule], }) export class AppModule {} ``` ### Option 2: Using Standalone Components You can directly import the directive and pipe in standalone components: ```ts import { CnicMaskDirective } from '@mdfrough/cnic-validator'; import { CnicFormatPipe } from '@mdfrough/cnic-validator'; import { CnicValidatorDirective } from '@mdfrough/cnic-validator'; @Component({ standalone: true, selector: 'app-cnic-form', imports: [CnicMaskDirective, CnicFormatPipe, CnicValidatorDirective], templateUrl: './cnic-form.component.html' }) export class CnicFormComponent {} ``` --- ## ๐Ÿงช Usage ### 1. Masked CNIC Input Field ```html <input type="text" formControlName="cnic" cnicMask placeholder="xxxxx-xxxxxxx-x" /> ``` ### 2. Reactive Form Setup ```ts this.form = this.fb.group({ cnic: ['', [Validators.required, cnicValidatorFn]] }); ``` ### 3. Template-Driven Form Setup ```html <input name="cnic" ngModel cnicMask cnicValidator #cnicModel="ngModel" /> @if(cnicModel.errors?.['invalidCnic']) { <div> Invalid CNIC format </div> } ``` ### 4. Display Validation Error (Reactive) ```html @if(form.get('cnic')?.errors?.['invalidCnic']) { <div> Invalid CNIC formats </div> } ``` ### 5. Pipe Usage ```html {{ '1234512345671' | cnic }} <!-- Outputs: 12345-1234567-1 --> ``` --- ## ๐Ÿ” API Reference ### Directive: `cnicMask` - Applies input masking as user types - Automatically formats input to `xxxxx-xxxxxxx-x` ### Directive: `cnicValidator` - Use in template-driven forms - Adds validation for correct CNIC structure ### Validator Function: `cnicValidatorFn` - Returns Angular `ValidatorFn` - Use in reactive forms ### Pipe: `cnic` - Formats raw CNIC into masked format --- ## ๐Ÿ›  Development Setup ```bash git clone https://github.com/your-org/cnic-validator-angular.git cd cnic-validator-angular npm install ng build cnic-lib --configuration production ng serve ``` The demo app will be available at `http://localhost:4200/`. --- ## ๐Ÿ“œ License [MIT](LICENSE) --- ## ๐Ÿค Contributing PRs welcome! Please follow Angular style guidelines and write tests. --- ## ๐Ÿ“ฃ Credits This project was built with โค๏ธ to simplify handling Pakistani CNICs in modern Angular apps.