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

后端 未结 4 1804
醉酒成梦
醉酒成梦 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:19

    Just a couple notes

    1) The above contains one redundant $. It should be ng-pattern="/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/"

    2) Don't remove leading ^ and trailing $ as it prevents from entering white spaces

    3) If you want to define the pattern in controller then

    $scope.passwordStrength = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/; and

    but never

    $scope.passwordStrength = "/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/";

    as the pattern is a RegExp object, not a string.

    Hope this will help you save some time.

提交回复
热议问题