2 years ago
#46829
Grigory Pavek
How to validate to prevent more then one whitespace? using Angular 2+ and Typescript
I need to validate to user can't type more than one whitespace on input type. I am find any solution for similar problem but not use case which i need .
Check this links:
https://www.titanwolf.org/Network/q/77d56d42-68ae-4a29-b239-0145da1c6852/y
Here is example for all cases but not for my.
I am try with:
import { AbstractControl, FormControl } from '@angular/forms';
export function removeSpaces(control: AbstractControl) {
const isSpace = (control.value || '').match('^[a-zA-Z\s-]+$');
return isSpace ? {'whitespace': true} : null;
}
and inside ts file:
public createForm() {
this.form = this.fb.group({
firstName: ['', [Validators.required, removeSpaces]]
});
}
This is work but only on start after no work.
Example when you have empty space you can't to set space but when set any characters you can type space so much time..
example write ( inside input type field ) John _______________ John2
I want to allow only two space
I need only example John__John__any-other-text.
__ is space just.
angular
forms
whitespace
0 Answers
Your Answer