【问题标题】:Why is there no error message appearing when using Yup's trim-method?为什么使用 Yup 的 trim-method 时没有出现错误信息?
【发布时间】:2022-10-16 00:00:33
【问题描述】:

我正在使用 Yup 和 Formik 作为我的注册表单。不知何故,在我的密码上使用 Yup 提供的修剪方法时,没有出现错误消息,而所有其他验证方法都正常工作。

这是我的 Yup 架构的密码部分:

password: Yup.string()
            .required('Password is required)
            .min(8, 'Password must be at least 8 characters long')
            .max(18, 'Password cannot be longer than 18 characters')
            .matches(
                /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])/,
                'Password must contain at least one number as well as one uppercase, lowercase, and special character'
            )
            .trim('Password cannot include leading and trailing spaces')
            .matches(
                /^(?!.*  )/,
                'Password cannot contain two or more consecutive spaces'
            )

【问题讨论】:

    标签: next.js formik yup


    【解决方案1】:

    验证必须在严格模式下运行,否则修剪方法将转换输入而不是验证它。如果添加了严格,它将检查是否有前导或尾随空格。此外,我发现如果在 html 输入字段中使用电子邮件类型,它可能会导致错误。设置类型电子邮件时,测试方法和修剪都不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多