本文介绍了获取当前及以后的Formik验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用Formik进行验证,即输入只能是currentYear或更高版本.
I need to get the validation using Formik that input should only be currentYear and up.
const currentYear = new Date().getFullYear();
expiryYear: yup
.string()
.required('Select expiry year')
.min(4, `Invalid year format (Example: ${currentYear + 4})`)
.max(4, `Invalid year format (Example: ${currentYear + 4})`)
.when('startDate', (currentYear, schema) => currentYear && schema.min(currentYear)),
推荐答案
请尝试以下操作:
yup.date()
.min(new Date().getFullYear(),
'Year must be current year or greater than current year');
检查演示
如果您想验证年份的长度,可以使用以下方法:
If you would like to validate the year's length, you can use this:
yup.number()
.test('len', 'Must be exactly 4 characters',
val => val && val.toString().length === 4 ).min(new Date().getFullYear());
这将首先验证年份的长度,然后检查它是否大于或等于当前年份.
这篇关于获取当前及以后的Formik验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!