3

I want to allow Joi to allow spaces/whitespaces in a title field of a form.

Working tomorrow with Jude.

should be allowed as wel as

Morningwalk

At this moment only the last one is validated as true. Here is my joi validation:

const schema = Joi.object().keys({
  title: Joi.string().alphanum().required().max(50),

I added Regex but without result.

title: Joi.string().alphanum().required().max(50), regex(
  new RegExp('^\w+( +\w+)*$'))

What is the right way?

Tichel
  • 481
  • 2
  • 10
  • 24

1 Answers1

3

The .alphanum() makes your check ignore whitespace. Also, when you define a regex using a constructor notation, you are using a string literal where backslashes are used to form string escape sequences and thus need doubling to form regex escape sequences. However, a regex literal notation is more convenient. Rather than writing new RegExp('\\d') you'd write /\d/.

So, you may use this to allow just whitespaces:

title: Joi.string().required().max(50), regex(/^\w+(?:\s+\w+)*$/)

However, you seem to want to not allow commas and allow all other punctuation.

Use

title: Joi.string().required().max(50), regex(/^\s*\w+(?:[^\w,]+\w+)*[^,\w]*$/)

Details

  • ^ - start of string
  • \s* - 0 or more whitespaces (or, use [^,\w]* to match 0 or more chars other than comma and word chars)
  • \w+ - 1 or more word chars (letters, digits or _, if you do not want _, replace with [^\W_])
  • (?:[^\w,]+\w+)* - zero or more repetitions of
    • [^\w,]+ - 1 or more chars other than comma and word chars
    • \w+ - 1 or more word chars
  • [^,\w]* - 0 or more chars other than comma and word chars
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • We are almost there. punctuation is still not working. – Tichel Mar 18 '19 at 13:43
  • @Tichel you have not asked about punctuation. You should update the question with what punctuation you want to match. Add valid/invalid cases. – Wiktor Stribiżew Mar 18 '19 at 13:45
  • @Tichel So, does `/^\w+(?:\W+\w+)*$/` work as expected? – Wiktor Stribiżew Mar 18 '19 at 13:57
  • @Tichel I see you also updated the example. But now, I do not quite understand what your intent is: what is NOT allowed? The pattern that will work for you is `/^\W*\w+(?:\W+\w+)*\W*$/`, see https://regex101.com/r/1Q2EcU/1. Do you want to disallow an empty string and match any non-empty/non-blank string? – Wiktor Stribiżew Mar 18 '19 at 14:27
  • Wiktor, a comma ( , ) is now allowed. A dot ( . ) not. I am working on it. – Tichel Mar 18 '19 at 14:28
  • @Tichel But you say `Working tomorrow with Jude.` is allowed and it contains a dot. What are the exact rules? Please add to the question. Also, see `/^\s*\w+(?:[^\w,]+\w+)*[^,\w]*$/` ([demo](https://regex101.com/r/1Q2EcU/2)). You may fork this fiddle with your examples if you need more and faster help. – Wiktor Stribiżew Mar 18 '19 at 14:29
  • Let me know what exactly works so that I could adjust the answer and explain in detail. – Wiktor Stribiżew Mar 18 '19 at 14:32
  • the answer with the demo /^\s*\w+(?:[^\w,]+\w+)*[^,\w]*$/ – Tichel Mar 18 '19 at 14:32
  • Great, I adjusted the answer and added the explanation. Please read the pattern details to see how you may adjust the pattern further if need be. – Wiktor Stribiżew Mar 18 '19 at 14:36