Angular2 custom validation, inspired by jQuery validation.
npm install ng2-validation --save
- required
- minlength
- maxlength
- pattern
- rangelength
- min
- max
- range
- digits
- number
- url
- date
- dateiso
First, you need use the latest form component, and disable deprecated forms.
import { bootstrap } from '@angular/platform-browser-dynamic';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
import { AppComponent } from './src/app.component';
bootstrap(AppComponent, [
disableDeprecatedForms(),
provideForms(),
]).catch(err => console.error(err));
First, import CUSTOM_FORM_DIRECTIVES
and REACTIVE_FORM_DIRECTIVES
, add them to component directives config.
import { Component } from '@angular/core';
import { REACTIVE_FORM_DIRECTIVES, } from '@angular/forms';
import { CUSTOM_FORM_DIRECTIVES } from 'ng2-validation';
@Component({
selector: 'app',
template: require('./app.html'),
directives: [REACTIVE_FORM_DIRECTIVES, CUSTOM_FORM_DIRECTIVES]
})
export class AppComponent implements OnInit {
}
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" rangelength="[5, 9]"/>
<p *ngIf="field.errors?.rangelength">error message</p>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" min="10"/>
<p *ngIf="field.errors?.min">error message</p>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" max="20"/>
<p *ngIf="field.errors?.max">error message</p>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" range="[10, 20]"/>
<p *ngIf="field.errors?.range">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" digits/>
<p *ngIf="field.errors?.digits">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" number/>
<p *ngIf="field.errors?.number">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" url/>
<p *ngIf="field.errors?.url">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" email/>
<p *ngIf="field.errors?.email">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" date/>
<p *ngIf="field.errors?.date">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" dateiso/>
<p *ngIf="field.errors?.dateiso">error message</p>
used like angular2 build-in validators.
import { Component } from '@angular/core';
import { REACTIVE_FORM_DIRECTIVES, FormGroup, FormControl } from '@angular/forms';
import { CUSTOM_FORM_DIRECTIVES, CustomValidators } from 'ng2-validation';
@Component({
selector: 'app',
template: require('./app.html'),
directives: [REACTIVE_FORM_DIRECTIVES, CUSTOM_FORM_DIRECTIVES]
})
export class AppComponent {
form: FormGroup;
constructor() {
this.form = new FormGroup({
field: new FormControl('', CustomValidators.range([5, 9]))
});
}
}
<input type="text" [formControl]="form.controls.field"/>
<p *ngIf="form.controls.field.errors?.rangelength">error message</p>
CustomValidators.rangeLength([5, 9])
CustomValidators.min(10)
CustomValidators.max(20)
CustomValidators.range([10, 20])
CustomValidators.digits
CustomValidators.number
CustomValidators.url
CustomValidators.email
CustomValidators.date
CustomValidators.dateiso
MIT