Reputation: 4390
I'm using:
+ (BOOL)isPassword:(NSString*)password {
NSString* pattern = @"^(?=.{6,20}$).*$";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
return [predicate evaluateWithObject:password];
}
But it is returning yes for ""
. Any tips?
Upvotes: 1
Views: 395
Reputation: 40211
As @Pfitz pointed out, you don't have a SELF
. That's used when filtering arrays for instance.
Try using NSRegularExpression
instead.
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"^(?=.{6,20}$).*$"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:password
options:0
range:NSMakeRange(0, [password length])];
if (match) {
NSRange range = [match range];
if (range.location != NSNotFound) {
// match
}
}
Upvotes: 2