【发布时间】:2020-10-18 00:34:33
【问题描述】:
我正在尝试使用 React 和 Formik 进行验证。我想实现最大位数只有 2,最大范围应该只有 12。
expiryMonth: yup.string().required('Select month').max(2, 'Invalid month format (Example: 06)'),
【问题讨论】:
标签: javascript reactjs formik yup
我正在尝试使用 React 和 Formik 进行验证。我想实现最大位数只有 2,最大范围应该只有 12。
expiryMonth: yup.string().required('Select month').max(2, 'Invalid month format (Example: 06)'),
【问题讨论】:
标签: javascript reactjs formik yup
你可以用这个:
yup.string()
.required("Select month")
.min(2, "Invalid month format (Example: 06)")
.max(2, "Invalid month format (Example: 06)")
.matches(/^(0?[1-9]|1[012])$/, "Month must be greater than 0 and can't be exceeded 12");
【讨论】:
number 类型,将其从number 更改为string。它会起作用的。