angular custom validator to validate a given age using momentjs
npm install ngx-age-validator
height="256px" width="256px" style="text-align: center;"
src="https://cdn.jsdelivr.net/gh/angular-material-extensions/select-country@master/assets/angular-material-extensions-logo.svg">










src="https://raw.githubusercontent.com/anthonynahas/ngx-age-validator/HEAD/assets/v1/ex1.gif">
If you like this project, support angular-material-extensions
by starring :star: and sharing it :loudspeaker:
- NgxAgeValidator used to validate the age for a given date
---
10.x---
``shell`
npm i -s ngx-age-validator
#### Errors
tooJung if the min argument > as the difference between the given date and todaytooOld if the min argument < as the difference between the given date and today
#### Use the library with reactive forms
`html`
`typescript
import {Component, OnInit} from '@angular/core';
import {FormControl, ValidationErrors} from "@angular/forms";
import {NgxAgeValidator} from "ngx-age-validator";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
title = 'ngx-age-validator-demo';
ageFormControl = new FormControl();
ngOnInit(): void {
this.ageFormControl = new FormControl(null, [NgxAgeValidator(18, 40)])
this.ageFormControl.valueChanges.subscribe(() => {
const controlErrors: ValidationErrors | null = this.ageFormControl.errors;
if (controlErrors != null) {
Object.keys(controlErrors).forEach(keyError => {
console.log(' keyError: ' + keyError + ', err value: ', controlErrors[keyError]);
});
}
})
}
}
`
src="https://raw.githubusercontent.com/anthonynahas/ngx-age-validator/HEAD/assets/v1/tooJung.png">
src="https://raw.githubusercontent.com/anthonynahas/ngx-age-validator/HEAD/assets/v1/tooOld.png">
Build the library
`bash`
$ npm run build:lib
Serve the demo app
`bash``
$ npm start
---
This project is supported by jetbrains with 1 ALL PRODUCTS PACK OS LICENSE incl. webstorm
---
Copyright (c) 2020 Anthony Nahas. Licensed under the MIT License (MIT)