Friday 7 April 2017

NG2- Custom Validation function for empty space checking.

NG2- Custom Validation function for empty space checking.
============================================
component.html
==============
<input type="text" [formControl]="NameCtrl" placeholder="Enter New name">
<div *ngIf="NameCtrl.touched" style="color:brown">
<span *ngIf="NameCtrl.status == 'INVALID'"> Enter  Name</span>
</div>
  
component.ts
==============
  
NameCtrl = new FormControl('', [Validators.required, this.NoWhitespaceValidator])


public NoWhitespaceValidator(control: FormControl) {
    let isWhitespace = (control.value || '').trim().length === 0;
    let isValid = !isWhitespace;
    return isValid ? null : { 'whitespace': true }
}

No comments:

Post a Comment