@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
Markdown
# 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.