2

I need regular expression to not allow only whitespaces in a field(the user should not enter just whitespaces in the field) but it can allow completely empty field and it can also allow string with whitespaces in between.

For example if I enter strings such as "This is a test" or "testing", the regular expression should pass validation. Also, If I don't enter anything in the field, it should pass validation. It should fail if I enter only whitespaces.

I tried the below mentioned ones but they fail.

1) ^[^-\s][a-zA-Z0-9_\s-]+$ - doesn't allow me to enter string with whitespaces in between

2) ^[^-\s][\w\s-]+$ - doesn't allow me to enter a string

suvenk
  • 467
  • 1
  • 5
  • 24
  • 1
    First, you should give examples of input and expected output. Then you should show us what you have tried and what fails. – Poul Bak Dec 02 '18 at 02:30
  • Hi. Sorry about that. I updated the question with the ones I already tried – suvenk Dec 02 '18 at 02:41

2 Answers2

6

Are spaces allowed at the beginning of your string?

If so, then something like that should work:

^$|.*\S+.*

Otherwise this one should do the trick:

^$|^\S+.*

Explanation:

I'll explain the version without leading spaces because the other one is just a slightly expanded version of it.

First, we check if the String is empty with ^$. This is explained in another question here Regular expression which matches a pattern, or is an empty string.

^ marks the beginning of the string which we want to check for a match.

\S+ checks for non-whitespace characters. The + makes sure, that there needs to be at least one of them or more. This includes spaces, tabs, new-lines, etc.

.* matches any characters (denoted by the dot) and any number of them or none (denoted by the *).


This all assumes, that you need to match the whole string though. If that is not the case for you, you could shorten the expression to:

^$|\S+

or

^$|^\S+

If the user input has matches for the first expression then you'll know the string he entered contains characters which are not space or is empty. If it has matches for the second expression you additionally know that it starts with non-space characters.

These expressions are of course only two of a lot of possible expressions. I chose them because they are easy to read and explain(in my opinion at least).

  • @suvenk I'm happy that I could help. Make sure to read the edit though. The new version takes tabs, new-lines and so on into account as well (not only spaces). – Etienne Rodriguez Dec 02 '18 at 03:26
  • I tried the edited one '^$|^\S+.* and it doesn't work. The one u posted before ^$|^[^ ]+.*$ seems to work fine. Could you please check the edited one once again – suvenk Dec 02 '18 at 04:07
  • @suvenk I tested it again on regex101.com and in javascript `var pattern = /^$|^\S+.*/; console.log(pattern.test("xyz"));` and it works for me. Could you share more of your code for using it? – Etienne Rodriguez Dec 02 '18 at 16:37
0

Does it need to be regex? If not, you could do:

    if (userInput.length === 0 || (userInput.length > 0 && userInput.trim().length)) {
        console.log("user input ok");
    }

Where userInput is what the user has entered.

userInput.trim() removes any white space from the beginning and end of the string. So if the length of that is 0, that will be false and mean the string was only spaces. Any spaces in between characters will be ok (userInput.trim() leaves those alone)

Michael S
  • 726
  • 1
  • 10
  • 23
  • I am using reactive forms validation of angular. So if user starts entering empty spaces, it should disable save button of the form and display error message below the field dynamically. If white spaces are removed, the error message should disappear dynamically. So I need a pattern to validate the fields – suvenk Dec 02 '18 at 03:02
  • That regex prevents me from entering just whitespaces but it also prevents me from entering a string or a string with spaces in between. So "Testing" and "This is a test" fail – suvenk Dec 02 '18 at 03:17
  • Yeah when I tried it a second time, it didn't work :/ – Michael S Dec 02 '18 at 03:18