gpt4 book ai didi

regex - 如何使用 express-validator npm 验证密码

转载 作者:IT老高 更新时间:2023-10-28 23:24:31 25 4
gpt4 key购买 nike

我正在使用 node 编写 rest API,表达 web 模块。对于验证,我使用 express-validator npm。我想在密码字段上应用一些验证规则。

如何使用 express-validator 实现它?

我想申请密码的验证规则为:

  1. 最少 8 个字符。
  2. 至少有一个大写字母。
  3. 至少一个小写字母。
  4. 至少有一个特殊字符。

我阅读了 this链接表明有一个名为 regex() 的函数可用。所以我尝试了它,但根本不工作。

我的做法:

req.check("password", "Password should be combination of one uppercase , one lower case, one special char, one digit and min 8 , max 20 char long").regex("/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/", "i");

错误

enter image description here

express-js 他们列出了所有方法,但没有找到解决我问题的方法/技巧。

最佳答案

您所指的链接已经有将近 3 年的历史了。此后,validator的API发生了变化。

要检查正则表达式,请使用 .matches():

req.check("password", "...").matches(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/, "i");

关于regex - 如何使用 express-validator npm 验证密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com