我正在使用 yup 库来验证输入字段中的值:
const dateToCheck = yup
.date()
.nullable()
.transform((curr, orig) => (orig === '' ? null : curr))
.required()
.min(minDate, 'Date cannot be this early')
.max(maxDate, 'Date too much in the future')
.test('format', 'Date is invalid', date => (date?.getFullYear() ?? 0) <= 9999)
我一直在尝试找出如何检查输入日期格式是否正确的方法?由于 yup 的工作方式,01.01.2000
和2000/01/01
都将通过 date
验证,但我希望只允许dd.MM.yyyy
格式或者yyyy/MM/dd
格式通过,而不是两者都可以通过,其他任何格式如果有效则标记为无效。在 yup 中正确的方法是什么?
.date()
时,.matches()
方法将无法工作。 - Alex T