In my current project, I'm working on validating a field in a reactive form. The tricky part is that the field can be left blank as it's not mandatory, but it shouldn't consist only of white space characters. To tackle this issue, I decided to create a custom validator for this specific scenario:
Custom-validator.ts
export function whiteSpaceValidator(control: AbstractControl): { [key: string]: any } | null {
const check = control.value.trim();
return check === '' ? { 'whiteSpaceCheck': { value: control.value } } : null;
}
However, I encountered a problem where the validation gets triggered even when the field is completely empty, which is not the desired behavior.
I'm now exploring alternative solutions to address this issue without resorting to iterating through each character in the string to determine if it's a white space character or not.