首页 > 解决方案 > 使用 Formik 反应验证最大范围

问题描述

我正在尝试使用 React 和 Formik 进行验证。我想实现最大位数仅为 2,最大范围应仅为 12。

expiryMonth: yup.string().required('Select month').max(2, 'Invalid month format (Example: 06)'),

标签: javascriptreactjsformikyup

解决方案


你可以使用这个:

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");


推荐阅读