简体   繁体   English

如果字符串在开始或结束时包含空格,则节点 js Joi 验证发送错误

[英]Node js Joi validation to send error if string contains space at start or end

How to create JOI validation with Node js if string contains empty space at start or end send error如果字符串在开始或结束时包含空格发送错误,如何使用 Node js 创建 JOI 验证

example: if input is示例:如果输入是
name = "test123" //valid name = "test123" //有效
name = "test(space)" or "(space)test" // invalid name = "test(space)" or "(space)test" // 无效

You can use a regex pattern to check that string does not start or end with space.您可以使用regex模式来检查字符串是否以空格开头或结尾。

Joi.string().pattern(new RegExp('^[^\s][^\s]+$', 'g'))

You can use below regex to validate the your cases您可以使用下面的正则表达式来验证您的案例

Joi.string().pattern(new RegExp('^([a-zA-Z0-9]+( [a-zA-Z0-9]+)*)$', 'g'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM