I am trying to insert a validation into my Angular 9 app which doesn't let a user insert loads of spaces instead of a text into a form.
Currently, I have the below code but it doesn't let for any space. I would like the user to be able to insert a space but not just all spaces as I need useful data in my database. This example is an enquiry form for my website. I have the below code with the custom empty space validator but as mentioned it doesn't let me insert any space. The aim is here to not allow users enter content such as 20 empty spaces or random repeated letters such as 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr'.
Here is the code snippet I have but I think it will need to be changed altogether:
import { AbstractControl, ValidationErrors } from '@angular/forms';
export function NoWhitespaceValidator(control: AbstractControl) : ValidationErrors | null {
if((control.value as string).indexOf(' ') >= 0) {
return {cannotContainSpace: true}
}
return null;
}
Only solutions that I seem to find are just validators that don't allow a single space.