javascript - JOI how to allow empty spaces and punctuation - Stack Overflow

I want to allow Joi to allow spaceswhitespaces in a title field of a form. Working tomorrow with Jude.

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

Share Improve this question edited Mar 18, 2019 at 13:46 Tichel asked Mar 18, 2019 at 13:23 TichelTichel 5312 gold badges12 silver badges26 bronze badges 6
  • 1 What if you replace new RegExp('^\w+( +\w+)*$') with /^\w+( +\w+)*$/? – Wiktor Stribiżew Commented Mar 18, 2019 at 13:24
  • still not allow spaces – Tichel Commented Mar 18, 2019 at 13:28
  • 1 Probably, you need to remove .alphanum() – Wiktor Stribiżew Commented Mar 18, 2019 at 13:29
  • Works. Now only add punctuation marks to regex. cheers – Tichel Commented Mar 18, 2019 at 13:33
  • So, you mean /^\w+(?:\W+\w+)*$/? – Wiktor Stribiżew Commented Mar 18, 2019 at 13:34
 |  Show 1 more ment

1 Answer 1

Reset to default 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 mas 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 ma 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 ma and word chars
    • \w+ - 1 or more word chars
  • [^,\w]* - 0 or more chars other than ma and word chars
  • $ - end of string.

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

相关推荐

  • javascript - JOI how to allow empty spaces and punctuation - Stack Overflow

    I want to allow Joi to allow spaceswhitespaces in a title field of a form. Working tomorrow with Jude.

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信