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,