admin管理员组文章数量:1356554
What I have
I'm trying to create a password field which validates the strength of the input, in another answer I found the regex
I can use to validate if the input meets specific conditions, the problem with this approach is that it throws only one error message for each of the validations.
password: yup
.string()
.required('Please Enter your password')
.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"
),
What i want
what I want is validate each of the characters that the user enters and throw the specific errors if each of the conditions are fulfilled, e.g if user inpunt is missing a lower case, it should throw the error 'must have a lower case', if the input is missing a number, it should throw the number 'must have a number' and so on. What I've tried it's to use match but this doesn't work:
password: yup
.string()
.required('Please Enter your password')
.matches(/^[a-z]+$/, 'One lowercase character')
.matches(/^[A-Z]+$/, 'One uppercase character')
.matches(/^\d+$/, 'One number')
// ... other validation
What I have
I'm trying to create a password field which validates the strength of the input, in another answer I found the regex
I can use to validate if the input meets specific conditions, the problem with this approach is that it throws only one error message for each of the validations.
password: yup
.string()
.required('Please Enter your password')
.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"
),
What i want
what I want is validate each of the characters that the user enters and throw the specific errors if each of the conditions are fulfilled, e.g if user inpunt is missing a lower case, it should throw the error 'must have a lower case', if the input is missing a number, it should throw the number 'must have a number' and so on. What I've tried it's to use match but this doesn't work:
password: yup
.string()
.required('Please Enter your password')
.matches(/^[a-z]+$/, 'One lowercase character')
.matches(/^[A-Z]+$/, 'One uppercase character')
.matches(/^\d+$/, 'One number')
// ... other validation
Share
Improve this question
edited Apr 13, 2021 at 12:41
Ajeet Shah
19.9k9 gold badges64 silver badges104 bronze badges
asked Apr 12, 2021 at 2:58
Cristian FlórezCristian Flórez
2,8118 gold badges35 silver badges62 bronze badges
2
- I'd highly remend a different password requirement. xkcd./936 Convoluted, hard-to-remember but short passwords are probably the worst approach there is – CertainPerformance Commented Apr 12, 2021 at 2:59
- understood, but I still want to know the approach to use to solve this kind of problems, individual regex validations with Yup and formik. – Cristian Flórez Commented Apr 12, 2021 at 3:09
1 Answer
Reset to default 11You just need to remove ^
(matches the beginning of input) and $
(matches the end of input) assertions from your code, like so:
password: Yup.string()
.required("Required")
.min(8, "Must be 8 characters or more")
.matches(/[a-z]+/, "One lowercase character")
.matches(/[A-Z]+/, "One uppercase character")
.matches(/[@$!%*#?&]+/, "One special character")
.matches(/\d+/, "One number"),
Here is a demo.
版权声明:本文标题:javascript - Creating a strong password with Yup and Formik with individual error messages - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743993147a2572516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论