So I want to validate that my input field to contain not just white space(spacebar) and special characters. I made a directive for white space here:
import { Directive } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidationErrors, AbstractControl } from '@angular/forms';
@Directive({
selector: '[noWhiteSpace]',
providers: [{ provide: NG_VALIDATORS, useExisting: NoWhitespaceDirective, multi: true }]
})
export class NoWhitespaceDirective implements Validator {
constructor() { }
validate(control: AbstractControl): ValidationErrors {
if(!control.value || control.value.trim() == '')
{
return {'required' : true };
}
return null;
}
}
I'm still figuring out for the special characters. Hope to find some solution here.