Saturday, July 30, 2022

[FIXED] How to validate 2 blank spaces with Javascript?

Issue

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.

The alert should be maintained by typing for example something like "Stack Code", not only does it appear when I leave the 2 spaces, but the alert is maintained if it remains that way. (it won't let me separate 'stack code' twice to show you)

From already thank you very much


Solution

You could you the regular expression match for that:

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


Answered By - Sergio Belevskij
Answer Checked By - Marie Seifert (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.