How to add password validation using regular expression in angularjs according to certain criterion?

后端 未结 4 1728
醉酒成梦
醉酒成梦 2021-02-20 16:33

I want to validate password entered by user for following criteria :

Password should be at least 8 characters long and should contain one number,one character an

4条回答
  •  北海茫月
    2021-02-20 17:14

    I took a stab at this with Angular's built-in pattern validator and was able to come up with the following that checks for:

    • At least 8 characters in length
    • Lowercase letters
    • Uppercase letters
    • Numbers Special characters

      password: [ '', [ Validators.required, Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}') ] ]

    I'll add that I'm by no means a regex expert. This is simply what worked for me for a closely related case to the OP. Maybe it will help someone else. Mozilla's documentation helped a lot in figuring this out, specifically the Writing a regular expression pattern section.

提交回复
热议问题