You can simply check the AbstractControl#hasError(...) method to see if the control has a specific error. FormGroup and FormControl are equal to AbstractControl s. for FormControl you simply pass the name of the error as an argument. for example
function regexValidator(control: FormControl): {[key:string]: boolean} { if (!control.value.match(/^pee/)) { return { 'badName': true }; } } <div *ngIf="!nameCtrl.valid && nameCtrl.hasError('badName')" class="error">Name must start with <tt>pee</tt>. </div>
The validator method should return a string / boolean map, where the key is the name of the error. This is the name you are checking in the hasError method.
For FormGroup you can pass the path to FormControl as an additional parameter.
<div *ngIf="!form.valid && form.hasError('required', ['name'])" class="error">Form name is required.</div>
name is simply the identifier of the FormControl for input.
Here is an example with validating FormControl and FormGroup .
import { Component } from '@angular/core'; import { FormGroup, FormBuilder, FormControl, Validators, AbstractControl, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; function regexValidator(control: FormControl): {[key:string]: boolean} { if (!control.value.match(/^pee/)) { return { 'badName': true }; } } @Component({ selector: 'validation-errors-demo', template: ` <div> <h2>Differentiate Validation Errors</h2> <h4>Type in "peeskillet"</h4> <form [formGroup]="form"> <label for="name">Name: </label> <input type="text" [formControl]="nameCtrl"/> <div *ngIf="!nameCtrl.valid && nameCtrl.hasError('required')" class="error">Name is required.</div> <div *ngIf="!nameCtrl.valid && nameCtrl.hasError('badName')" class="error">Name must start with <tt>pee</tt>.</div> <div *ngIf="!form.valid && form.hasError('required', ['name'])" class="error">Form name is required.</div> </form> </div> `, styles: [` .error { border-radius: 3px; border: 1px solid #AB4F5B; color: #AB4F5B; background-color: #F7CBD1; margin: 5px; padding: 10px; } `], directives: [REACTIVE_FORM_DIRECTIVES], providers: [FormBuilder] }) export class ValidationErrorsDemoComponent { form: FormGroup; nameCtrl: AbstractControl; constructor(formBuilder: FormBuilder) { let name = new FormControl('', Validators.compose([ Validators.required, regexValidator ])); this.form = formBuilder.group({ name: name }); this.nameCtrl = this.form.controls['name']; } }
UPDATE
Ok, so I got it, but it's a little detailed. I could not figure out how to properly access individual FormControl inputs. So what I did, just created a link to ForgGroup
<form #f="ngForm" novalidate>
Then, in order to verify the validity, I simply use the hasError overload, which went the way to the form control name. For <input> , which use name and ngModel , the value name added to the main FormGroup with this name as the name of FormControl . This way you can access it, for example
`f.form.hasError('require', ['nameCtrl'])`
subject to name=nameCtrl . Pay attention to f.form . f is an instance of NgForm that has a FormGroup form member FormGroup .
Below is a refactored example
import { Component, Directive } from '@angular/core'; import { FormControl, Validators, AbstractControl, NG_VALIDATORS, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; function validateRegex(control: FormControl): {[key:string]: boolean} { if (!control.value || !control.value.match(/^pee/)) { return { 'badName': true }; } } @Directive({ selector: '[validateRegex]', providers: [ { provide: NG_VALIDATORS, useValue: validateRegex, multi: true } ] }) export class RegexValidator { } @Component({ moduleId: module.id, selector: 'validation-errors-template-demo', template: ` <div> <h2>Differentiate Validation Errors</h2> <h4>Type in "peeskillet"</h4> <form #f="ngForm" novalidate> <label for="name">Name: </label> <input type="text" name="nameCtrl" ngModel validateRegex required /> <div *ngIf="!f.form.valid && f.form.hasError('badName', ['nameCtrl'])" class="error">Name must start with <tt>pee</tt>.</div> <div *ngIf="!f.form.valid && f.form.hasError('required', ['nameCtrl'])" class="error">Name is required.</div> </form> </div> `, styles: [` .error { border-radius: 3px; border: 1px solid #AB4F5B; color: #AB4F5B; background-color: #F7CBD1; margin: 5px; padding: 10px; } `], directives: [REACTIVE_FORM_DIRECTIVES, RegexValidator] }) export class ValidationErrorsTemplateDemoComponent { }