简体   繁体   English

节点打字稿:正则表达式允许至少一个带有 joi 验证的字母字符的空格

[英]node typescript : regex to allow whitespace with at least one alphanum caracter with joi validation

I am using joi and regex to validate user input, I would like to accept only alpha num caracters where whitespace can be allowed.我正在使用 joi 和 regex 来验证用户输入,我只想接受可以允许空格的字母数字字符。 I've done the following :我做了以下事情:

const schema = Joi.object({
  value: Joi.string()
    .regex(
      /^[a-zA-Z0-9 ]*$/,
      'message',
    )
    .max(100)
    .required()
...

The problem is that now I allow user to fill a full string composed only of whitespaces, and that's not what I want, I accept whitespaces, only if they are between alphanum caracters.问题是现在我允许用户填充仅由空格组成的完整字符串,这不是我想要的,我接受空格,只有当它们在字母字符之间时。 I am using this to test : https://www.regextester.com/104025我用它来测试: https : //www.regextester.com/104025

Try this one:试试这个:

/^[a-zA-Z0-9]+[a-zA-Z0-9 ]*[a-zA-Z0-9]+$/

I dont know if this is the easiest or most elegant way but [a-zA-Z0-9]+ matches one or more of [a-zA-Z0-9] using the +我不知道这是最简单的还是最优雅的方式,但[a-zA-Z0-9]+匹配一个或多个[a-zA-Z0-9]使用+

I added this to the front and end of your regex.我将此添加到正则表达式的前端和末尾。

Test and explanation: https://regex101.com/r/79uEWb/2/测试与说明: https : //regex101.com/r/79uEWb/2/

Specify at the beginning that space is not allowed [^ ]在开头指定不允许使用空格[^ ]

 const withSpaceInFront = " hello"; const text = "hello there" const textAndNumbers = "hello 5" const textWithSpecialChars = "hello#" const numeric = 123 const textWithSpecialCharsAllowed = "general Kenobi..?" const regexNoSpace = /^[^ ][a-zA-Z0-9 ]*$/; console.log("no space in front") console.log(regexNoSpace.test(withSpaceInFront)); console.log(regexNoSpace.test(text)); console.log(regexNoSpace.test(textAndNumbers)); console.log(regexNoSpace.test(textWithSpecialChars)); console.log(regexNoSpace.test(numeric)); //if you want to accept special chars like ? or . just add them in your list of accepted chars const regexAllowedSpecialChars = /^[^ ][a-zA-Z0-9 ?.]*$/; console.log("no space in front & specific chars allowed") console.log(regexAllowedSpecialChars.test(textWithSpecialCharsAllowed));

Regex tests: https://regex101.com/r/8Ulpu9/1/正则表达式测试: https : //regex101.com/r/8Ulpu9/1/

暂无
暂无

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

相关问题 如果字符串包含“”(空格),如何使Joi regex()验证失败? - How to make Joi regex() validation fail if the string contains “ ” (whitespace)? 电话验证正则表达式需要允许空格和括号 - Phone validation regex needs to allow whitespace and parantheses 用于验证用户名的正则表达式:允许带有至少一个字符的字符,数字和特殊字符(-_。) - regex for username validation : allow chrarchters , numbers and special chars(-_.) with at least one charachter 至少匹配一个字母,一个数字和一个非字母数字 - Match at least one letter, one number and one non-alphanum 正则表达式:必须至少包含一个数字和字母,但不能包含其他字符/空格 - RegEx: Must have at least one number and letter but no other characters / whitespace Joi 验证 || 在 object 的数组中,至少有一个 object 应该包含某个键的特定值 - Joi Validation || In an array of object, at least one object should contain a particular value of a key 如何在正则表达式中允许空格 - How to allow whitespace in Regex javascript regex 允许至少一个特殊字符和一个数字 - javascript regex to allow at least one special character and one numeric Joi 验证:有没有办法在一个 go 中允许多个模式对象的未知键 - Joi Validation: Is there a way to allow unknown keys for several schema objects in one go 使用正则表达式在至少一个数字后允许逗号 - Allow comma after at least one number using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM