【发布时间】:2020-12-23 15:43:24
【问题描述】:
我正在尝试验证用户输入的密码。密码的最小长度应为 4,并且应包含字符和数字。我正在使用 Joi npm 模块进行验证。
app.js 代码:
const schema = Joi.object({
email: Joi.string().email().required(),
username: Joi.string().alphanum().min(3).required(),
password: Joi.string().min(4).required(),
});
app.post('/register',async (req,res)=>{
try{
const value = await schema.validateAsync({
email: req.body.email,
username: req.body.username,
password: req.body.password,
});
}catch(e){
console.log(e)}
})
如何检查用户密码中是否同时包含字符和数字?我在这里没有使用正则表达式。有人可以帮忙吗?
【问题讨论】:
-
你可以尝试使用
Regex-->password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$'))`
标签: node.js express validation joi