【问题标题】:multiple regex/pattern validation Joi Hapi多个正则表达式/模式验证 Joi Hapi
【发布时间】:2020-09-19 17:46:58
【问题描述】:

大家好,我想用 joi/hapi 进行多重正则表达式验证,但我不明白怎么做我尝试了 1 个数字和 1 个特殊字符:

const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .pattern(new RegExp('^[a-z]{1,}$'))
      .pattern(new RegExp('^[A- Z]{1,}$'))
      .pattern(new RegExp('^[0 - 9]{1,}'))
      .pattern(new RegExp('^[!@#$%&*]{1,}'))
      .min(8)
      .required()
  });
  return schema.validate(data);
};

const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .regex('^[a-z]{1,}$')
      .regex('^[A- Z]{1,}$')
      .regex('^[0 - 9]{1,}')
      .regex('^[!@#$%&*]{1,}')
      .min(8)
      .required()
  });
  return schema.validate(data);
};

我该怎么做?

【问题讨论】:

    标签: javascript regex joi hapi


    【解决方案1】:

    regex() 要求对象是 RegExp 对象。

    并在正则表达式中修复

    https://javascript.info/regexp-anchors

    工作演示

    https://stackblitz.com/edit/js-7c78r7?file=index.js

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2021-04-20
      • 2018-05-25
      • 2021-06-13
      • 2019-01-06
      • 1970-01-01
      • 2017-03-03
      • 2018-09-24
      • 1970-01-01
      相关资源
      最近更新 更多