[英]Can we add custom validations in formik YupValidationSchema?
Can we add custom validations in formik YupValidationSchema which I have mentioned below ?我们可以在我下面提到的 formik YupValidationSchema 中添加自定义验证吗?
YupValidationSchema = () => {
return Yup.object({
Email: Yup.string()
.max(256, "Length exceed 256 chars")
.matches(EMAIL_REGEXP, "Enter a valid email address")
.required("Email is required")
})
}
I need to add one more validation for the email field like it should accept certain domains我需要为电子邮件字段添加一项验证,例如它应该接受某些域
let domainVal = companyName.some((val) => email.includes(val));
if(!domainVal) error('Invalid')
else success('Valid');
can someone help me how can we add this domain validation code in yupvalidationschema?有人可以帮我如何在 yupvalidationschema 中添加这个域验证代码吗?
You can use the Yup test method
to add a custom validation to your Email您可以使用
Yup test method
向您的电子邮件添加自定义验证
Basic syntax: .test("test-name", "Error message", function)
基本语法:
.test("test-name", "Error message", function)
return Yup.object({
Email: Yup.string()
.max(256, "Length exceed 256 chars")
.matches(EMAIL_REGEXP, "Enter a valid email address")
.required("Email is required")
.test("email-include-domain", "Email Must include domain", (value) => companyNames.some((company) => value.includes(company));
Related Stackoverflow Post: How to get Yup to perform more than one custom validation?相关 Stackoverflow 帖子: 如何让 Yup 执行多个自定义验证?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.