Skip to content

Instantly share code, notes, and snippets.

@jenhausu
Created September 23, 2016 05:42
Show Gist options
  • Select an option

  • Save jenhausu/18abe6819b02a65898c8670ba18d8d0d to your computer and use it in GitHub Desktop.

Select an option

Save jenhausu/18abe6819b02a65898c8670ba18d8d0d to your computer and use it in GitHub Desktop.
+ (BOOL)password:(NSString *)string {
if ([self checkFormat:string withPattern:@"^([a-zA-Z]+\\d+|\\d+[a-zA-Z]+)[a-zA-Z0-9]*$" matchingOptions:NSRegularExpressionCaseInsensitive]) {
if (string.length >= 6 && string.length <= 12) {
for (NSInteger l = 0 ; l < string.length - 2 ; l++ ) {
NSString *subString = [string substringWithRange:NSMakeRange(l, 3)];
// 檢查數字
for (NSInteger i = 0 ; i <= 9 ; i++ ) {
if ([subString isEqualToString:[NSString stringWithFormat:@"%ld%ld%ld", i, i, i]]) { // 不可連續三個重複的數字
return NO;
} else if ([subString isEqualToString:[NSString stringWithFormat:@"%ld%ld%ld", i, i + 1, i + 2]]) { // 不可連續三個連續的數字(123, 234...等)
return NO;
}
}
// 檢查字母
for (char alphabet = 'a' ; alphabet <= 'z' ; alphabet++ ) {
if ([subString isEqualToString:[NSString stringWithFormat:@"%c%c%c", alphabet, alphabet, alphabet]]) { // 檢查有無連續三個一樣的小寫英文字母
return NO;
} else if ([subString isEqualToString:[NSString stringWithFormat:@"%c%c%c", alphabet, alphabet + 1, alphabet + 2]]) { // 檢查有無連續三個連續的小寫英文字母
return NO;
}
}
for (char alphabet = 'A' ; alphabet <= 'Z' ; alphabet++ ) {
if ([subString isEqualToString:[NSString stringWithFormat:@"%c%c%c", alphabet, alphabet, alphabet]]) { // 檢查有無連續三個一樣的大寫英文字母
return NO;
} else if ([subString isEqualToString:[NSString stringWithFormat:@"%c%c%c", alphabet, alphabet + 1, alphabet + 2]]) { // 檢查有無連續三個連續的大寫英文字母
return NO;
}
}
}
return YES;
}
}
return NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment