0

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.

  • so, all you have to find is 2 consecutive space, don't you ? `isMultipleSpaces = (control.value || '').match('^.*\s\s.*$');` – Random Jan 13 '22 at 13:44
  • Yes I need to prevent twice space in same time... Example space space not correct, but space then example Mark space Mark space is allowed. space ( is keyboard space ) – Grigory Pavek Jan 13 '22 at 13:53
  • yeah. So you want to raise an error if you see 2 consecutive spaces. That's what I'm saying. If you see 2 spaces without any other character between them, this is an error. – Random Jan 13 '22 at 13:54
  • I do not want to set error... Just to prevent to set 2 spaces without any other caracter – Grigory Pavek Jan 13 '22 at 14:39
  • eh ? You're writting a validator. A validator returns an error if your input does not fit. So here, the validator will allow you to write a text to the user "Please do not write multiple consecutive whitespaces". If you want to fix the input programmatically, you'll have to change the value everytime the user types something, which is not made by validators, but by listeners. What do you want exactly ? – Random Jan 13 '22 at 14:43
  • I want the listeners.... Like a pattern for preventi type space twice. Like a directive? – Grigory Pavek Jan 13 '22 at 14:48
  • indeed, a directive should be ok. Have to test it, if it does not conflicts with the FormControl (if the user types a 2nd space, the form should not be updated, so the event listener have to catch it before the form...). Have a look to [this answer](https://stackoverflow.com/a/47474444/4786273) – Random Jan 13 '22 at 14:52
  • The answers are similar but none are what I need. – Grigory Pavek Jan 13 '22 at 15:01
  • the answer listed allows you to prevent any keyboard event. In your case, you want to prevent whitespace, but under certain condition. So all you have to do, it take this solution, find a way to gather the input whole value, and if the last char of the input is a whitespace, prevent further whitespaces. That's not very different. If you're not able to implement this solution, please write an other question, which will be more precise: "How to prevent user from typing multiple consecutive whitespaces in an input" – Random Jan 13 '22 at 15:24
  • Okkk nicee i wil trt – Grigory Pavek Jan 13 '22 at 15:26

0 Answers0