4

I am using formik with yup to validate a form. In one input it passes if it contains only spaces, which is not valid for my use-case. So I went through Google to find some kind of regex so if the string only has spaces it would throw some message, but I didn't find anything.

nome: string()
      .min(3, '* Deve conter no mínimo 3 caracteres')
      .required('* Este campo é obrigatório')
      .matches(/[^\s*].*[^\s*]/g, '* This field cannot contain only blankspaces'),

The problem with this is that when it reaches 3 characters even with required yup condition the validation doesn't work, and matches with regex don't block the spaces. So far I managed a work-around with: nome: values.nome.trim().replace(/\s+/g, ' ') But with the correct regex expression I can throw the error in real time.

7
  • Try /^\S+$/ if you need to match a whole string that has no whitespace Commented May 13, 2020 at 17:42
  • Please provide an example input / output Commented May 13, 2020 at 17:42
  • Some kinda of : space.next === someCaracter ? valid : notValid logic and this "space next" would be looked by some regex express Commented May 13, 2020 at 17:42
  • Are you just making sure that the field does not contany any white space? In this case why not simply matchin for \s+ ? Commented May 13, 2020 at 17:43
  • @WiktorStribiżew it worked if the whole string has no whitespace but if I wanna some kinda of example: fullname= Alex martines ? Commented May 13, 2020 at 18:05

3 Answers 3

10

You may use

/^(?!\s+$).*/
/^(?!\s+$)/
/^\s*\S.*$/
/^\s*\S[^]*$/
/^\s*\S[\s\S]*$/

The first two regexps will match any string that is not equal to 1 or more whitespaces.

The last third, fourth and fifth match a string that contains at least one non-whitespace char.

See the regex demo

Details

  • ^ - start of string
  • (?!\s+$) - a negative lookahead that fails the match if there are 1 or more whitespaces and end of string immediately to the right of the current location
  • .* - 0 or more chars other than line break chars, as many as possible
  • \s*\S.* - zero or more whitespaces, a non-whitespace, and then any zero or more chars other than line break chars
  • \s*\S[^]* / \s*\S[\s\S]* - same as above but any chars including line breaks can appear after the non-whitespace char
  • $ - end of string
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Perfect answer
3

Since you want to match in case any white space is present please try:

nome: string()
      .min(3, '* Deve conter no mínimo 3 caracteres')
      .required('* Este campo é obrigatório')
      .matches(/^(\S+$)/g, '* This field cannot contain only blankspaces'),

3 Comments

Sorry, i mistook the error message, if the field has only white spaces then its not valid
.matches(/^(\S+$)/, '* This field cannot contain only blankspaces') This worked for me, thanks!
I am glad it worked for you :) Please don't forget to upvote the answer and / or choose it as final answer.
1

.matches(/^(\S+$)/, '* This field cannot contain only blankspaces') worked for me as well.

Mark the capital S but not the small s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.