我正在尝试使用以下方法将输入字段验证为网站
yup.string().url()
Run Code Online (Sandbox Code Playgroud)
但似乎如果不发送协议,它会出错,例如,当网站应该灵活地接受时 stackoverflow.com
请帮忙
因此,当我尝试使用 formik 处理我的输入字段时,我遇到了一些问题,基本上在按下一个输入字段后,其余输入字段验证就会开始,我不希望这种情况发生。
最好是第一次验证仅在提交时进行,然后在更改后继续进行验证。
这是我的代码:
逻辑:
const CompanyProfile = () => {
const CompanySchema = Yup.object().shape({
name: Yup.string()
.min(2, 'too short')
.required(ERROR_REQUIRED),
industry: Yup.string().notRequired(),
address: Yup.string().notRequired(),
crn: Yup.number().required(ERROR_REQUIRED),
website: Yup.string().notRequired(),
employeesNbr: Yup.string().required(ERROR_REQUIRED),
phoneNumber: Yup.number().required(ERROR_REQUIRED),
userRole: Yup.string().required(ERROR_REQUIRED),
personCheck: Yup.boolean().required(ERROR_REQUIRED),
});
const formik = useFormik({
initialTouched: false,
validateOnChange: true,
initialValues: {
name: '',
industry: '',
address: '',
crn: '',
website: '',
employeesNbr: '',
phoneNumber: '',
userRole: '',
personCheck: false,
},
validationSchema: CompanySchema,
onSubmit: values => {
alert(JSON.stringify(values, null, 2));
},
});
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试验证一个字符串,如果为空则不应验证,但如果有字符则应开始验证。我尝试过,但没有成功
iban: yup
.string()
.notRequired()
.min(5, 'Minimum of 5')
.max(34, 'Maximum of 34'),
Run Code Online (Sandbox Code Playgroud)
如果 '' 中的空字符串仍然抛出至少 5 个错误,我该如何解决这个问题?