1

I am trying to validate a password through the usage of regex with objective-c. I have searched through about 4-5 different stack overflow answers on this topic and for some reason it still gives me the same outcome.

The string needs to contain a minimum of:

  • 8 characters in the string {8,20} (a maximum of 20 characters)
  • 1 uppercase character [A-Z]
  • 1 lowercase character [a-z]
  • 1 number [0-9] ! the order of these must be non-specific

This could also be a logic error, which it would tear my soul apart, been spending a few hours on this. I am 'fairly' certain it isn't but well.. it could be.

Here is my validation function:

- (BOOL) validPassword:(NSString *) passwordString {
    NSString *passwordRegex = @"(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{8,20}"; //regex string condition
    NSPredicate *passwordValidation = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
    return [passwordValidation evaluateWithObject:passwordRegex];
}

Here I am calling the function:

if (![self validPassword:self.passwordTextField.text]) { //if password is invalid
        //display feedback to user
    } else { //if password is valid
        //create a user
    }

Thank you in advance!

Cheers,

7
  • it still gives me the same outcome - what is the outcome? Do you want to match Uu8;)! iyhoiui>< string?If yes, replace [a-zA-Z0-9] with .. Commented Apr 27, 2018 at 7:44
  • If I had for example the following string: "ASDqwe123" it should technically work, correct? Commented Apr 27, 2018 at 7:49
  • It will match your regex. Commented Apr 27, 2018 at 7:50
  • So I thought as well.. But it is still telling me that the string doesn't match the regex. Commented Apr 27, 2018 at 7:51
  • Then the problem is with your code, not the regex. Commented Apr 27, 2018 at 7:52

1 Answer 1

1

You are checking if the pattern is compatible with itself instead of checking the password:

return [passwordValidation evaluateWithObject:passwordRegex];

=>

return [passwordValidation evaluateWithObject:passwordString];

If you'd have named your variables differently:

- (BOOL)validPassword:(NSString *)stringToValidate {
    NSString *pattern = @"(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{8,20}"; //regex string condition
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    return [predicate evaluateWithObject:stringToValidate];
}

It might have been more obvious.

Since you are in a small method with 2 vars & 1 params, naming your var starting with the same sequence (passwordFollwedBySomething), it's harder to see when you misplaced one of them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.