If you want to see if both your regex qualifies as well as whether the password matches the original password, you generally would not do the latter via regex. You should just check for the string equality with NSString
method isEqualToString
.:
- (BOOL)validateString:(NSString *)input password:(NSString *)password
{
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^$|^[^[:space:]]{6,20}$" options:0 error:&error];
if ([regex numberOfMatchesInString:input options:0 range:NSMakeRange(0, [password length])] > 0 && [input isEqualToString:password]) {
return YES;
}
return NO;
}
Having said that, I would not recommend checking both the regex and the string equality. And one of two situations will arise: Either they're inconsistent (because the password you're validating against doesn't, itself, pass the regex) or they're redundant (because if the password you're validating against satisfies the regex, then all you need to check is the equality of the strings).
Generally, I'd do some validation, such as your regex, for new passwords, but when validating against another password, I'd just check for equality only.