Giter Club home page Giter Club logo

ngx-age-validator's Introduction

angular-material-extensions's logo

ngx-age-validator - Angular custom validator to validate a given age using momentjs

npm version demo docs: typedoc Build Status Join the chat at https://gitter.im/ngx-age-validator/Lobby dependency Status devDependency Status npm Greenkeeper badge license

We assume the following example: minimum age: 18 years old - maximum age 40 years old

@angular-material-extensions/select-country demonstration

Built by and for developers ❤️

Do you have any question or suggestion ? Please do not hesitate to contact us! Alternatively, provide a PR | open an appropriate issue here

If you like this project, support angular-material-extensions by starring ⭐ and sharing it 📢

Table of Contents

Library's validator

  • NgxAgeValidator used to validate the age for a given date

  • Angular developed and tested with 10.x

npm i -s ngx-age-validator

Errors

tooJung if the min argument > as the difference between the given date and today tooOld if the min argument < as the difference between the given date and today

Use the library with reactive forms

<mat-form-field appearance="outline">
      <mat-label>Choose a date</mat-label>
      <input matInput [matDatepicker]="picker" [formControl]="ageFormControl">
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-datepicker #picker></mat-datepicker>
      <mat-error>Error</mat-error>
      <mat-error *ngIf="ageFormControl.hasError('tooJung')">You are too jung!</mat-error>
      <mat-error *ngIf="ageFormControl.hasError('tooOld')">You are too old!</mat-error>
</mat-form-field>
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]);
        });
      }

    })
  }

}

@angular-material-extensions/select-country demonstration

@angular-material-extensions/select-country demonstration

Build the library

$ npm run build:lib

Serve the demo app

$ npm start

Other Angular Libraries


Built by and for developers ❤️ we will help you 👊


jetbrains logo

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.