React Validation Max Range Using Formik

Viewed 629

I'm trying to make a validation using React and Formik. I wanted to achieve that the max digit is only 2 and the max range should be only to 12.

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

You can use this:

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

Related