1

I'm trying to validate password, but i have a problem. For now, I have an input with next validators:

<form name="RegisterForm">
    <input type="password" name="password"
                           ng-minlength="8"
                           ng-maxlength="20"
                           space-restricted
                           numbers-required
                           lowercase-required
                           uppercase-required
                           special-characters-required
                           password-validator
                           required>
</form>

Here is the sample of lowercase validator:

function lowercaseRequired() {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: link
    };
}

function link(scope, element, attrs, ngModel) {
    ngModel.$parsers.unshift(function(value) {
        var validationPattern = /(?=.*[a-z])|^$/,
            isValid = validationPattern.test(value);

        ngModel.$setValidity('lowercaseRequired', isValid);
        return isValid ? value : undefined;

}

How i can handle minimum three out of four validations?

  • lowercase
  • uppercase
  • special characters
  • numbers

I thought about big regexp, but it was a bad idea, I think. As a result, I need to get a password that contains : min 8 chars, max 20 chars, doesn't contain whitespaces, and contains minimum three out of four validations.

Ihor Yanovchyk
  • 768
  • 6
  • 13

1 Answers1

0

Here is the answer: ^((?=.*[\d])(?=.*[a-z])(?=.*[A-Z])|(?=.*[a-z])(?=.*[A-Z])(?=.*[^\w\d\s])|(?=.*[\d])(?=.*[A-Z])(?=.*[^\w\d\s])|(?=.*[\d])(?=.*[a-z])(?=.*[^\w\d\s])).{8,20}$

REGEXR

Where:

  • ^ - beginning of line anchor
  • ((?=.*[\d]) - check for digits
  • (?=.*[a-z]) - check for lower case
  • (?=.*[A-Z]) - check for upper case
  • |(?=.*[a-z]) (?=.*[A-Z]) (?=.*[^\w\d\s]) - check for special chars
  • |(?=.*[\d]) (?=.*[A-Z]) (?=.*[^\w\d\s])|(?=.*[\d]) (?=.*[a-z]) (?=.*[^\w\d\s]))\S{8,20} - match any character ('\S' expect whitespace) from 8 start char at the end 20 of line anchor
Ihor Yanovchyk
  • 768
  • 6
  • 13