regex - Regular expression in javascript to not allow only whitespaces in a field but allow string with whitespaces and also all

I need regular expression to not allow only whitespaces in a field(the user should not enter just white

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 pletely 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

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 pletely 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

Share Improve this question edited Dec 2, 2018 at 2:47 suvenk asked Dec 2, 2018 at 2:21 suvenksuvenk 5171 gold badge11 silver badges27 bronze badges 2
  • 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 Commented Dec 2, 2018 at 2:30
  • Hi. Sorry about that. I updated the question with the ones I already tried – suvenk Commented Dec 2, 2018 at 2:41
Add a ment  | 

3 Answers 3

Reset to default 7

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).

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)

you need to simple regex .*\S.*

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742350336a4427387.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信