PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

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

 July 30, 2022     javascript, reactjs, validation     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing