如何根据区域设置使用 Formik 和 Yup 设置不同的密码要求?
How to set different password requirements with Formik and Yup depending on locale?
我正在处理一个网站,根据国家/地区的不同,我有不同的密码要求,有人可以帮我解决这个问题吗?这是我到目前为止的进展,但它仍然只需要 6 个字符,没有别的。
password: Yup.string()
.when({
is: locale => locale === 'mt',
then: Yup.string().matches(
/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/,
'Must Contain 8 Characters, One Uppercase, One Lowercase, One Number and one special case Character'
),
otherwise: Yup.string().min(6, 'Must containt at least 6 characters')
})
.required(intl.formatMessage({ id: 'inputs.password.required' }))
});
locale 是国家/地区,在 url 中设置。它正确获取语言环境。
你就不能用这样的东西吗:
password: locale === 'mt' ?
Yup.string().matches(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/,
'Must Contain 8 Characters, One Uppercase, One Lowercase, One Number and one special case Character'
) : Yup.string().min(6, 'Must containt at least 6 characters')
我正在处理一个网站,根据国家/地区的不同,我有不同的密码要求,有人可以帮我解决这个问题吗?这是我到目前为止的进展,但它仍然只需要 6 个字符,没有别的。
password: Yup.string()
.when({
is: locale => locale === 'mt',
then: Yup.string().matches(
/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/,
'Must Contain 8 Characters, One Uppercase, One Lowercase, One Number and one special case Character'
),
otherwise: Yup.string().min(6, 'Must containt at least 6 characters')
})
.required(intl.formatMessage({ id: 'inputs.password.required' }))
});
locale 是国家/地区,在 url 中设置。它正确获取语言环境。
你就不能用这样的东西吗:
password: locale === 'mt' ?
Yup.string().matches(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/,
'Must Contain 8 Characters, One Uppercase, One Lowercase, One Number and one special case Character'
) : Yup.string().min(6, 'Must containt at least 6 characters')