I need a regular expression that can validate following cases,
- Be 8 to 20 characters
- Use at least 1 upper case letter, 1 lower case letter, and 1 number
- Not repeat the same number or letter more than 3 times in a row
- Not contain spaces, and may only use these characters @ # * ( ) + = { } / ? ~ ; , . - _
The closest solution I can find is
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z])(?=.*[@(#*){+=}/?~;,._]).{8,20}$
But contains following issue,
- Accepting space
- Can't add
-
character - Repeating same number or letter more than 3 times in a row.
EDIT: Fixed the repeating character issues and have final expression like,
^(?!.*?(.)\1{3})(?!.* )(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?!.*[:£€&"!'[\]%^\|<>$]).{8,20}$
But its only working on online tester like [RegExr][1]
I tried following code but it generates runtime
- (BOOL)string:(NSString *)text matches:(NSString *)pattern{
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];
return matches.count > 0;
}