Home > Software design >  How to validate 2 blank spaces with Javascript?
How to validate 2 blank spaces with Javascript?

Time:07-15

Good afternoon, I have a validation done for when the user leaves only 1 blank space, the idea would be that if you want to enter a word with 2 blank spaces, do not leave it.

Here is the code I have:

    if(value && value.length > 1 && value[value.length-1] === ' ') {
       return Promise.reject('Please remove trailing blanks');
    }

'value' returns what the user entered character by character.

I am trying to understand this logic as it has been done by another developer.

From already thank you very much

CodePudding user response:

To check if there are 2 blank spaces at the end you can use:

value.endsWith("  ")

which returns true or false

CodePudding user response:

You could you the regular expression match for that:

value.match(/\s{2,}$/)

CodePudding user response:

For checking 2 blank space you can do this:

if(value && value.length > 1 && value.endsWith(" ")) { return Promise.reject('Please remove trailing blanks'); }

  • Related