Last active
December 4, 2018 15:57
-
-
Save didats/7521151 to your computer and use it in GitHub Desktop.
Civil ID Validation for Kuwait Citizen in Objective C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(BOOL) validateCivilID:(NSString *)civilID { | |
bool valid = false; | |
NSString *regex = @"[0-9]{12}"; | |
NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; | |
if ([test evaluateWithObject:civilID]) { | |
double test = 11 - (([[civilID substringWithRange:NSMakeRange(0, 1)] integerValue] * 2) + | |
([[civilID substringWithRange:NSMakeRange(1, 1)] integerValue] * 1) + | |
([[civilID substringWithRange:NSMakeRange(2, 1)] integerValue] * 6) + | |
([[civilID substringWithRange:NSMakeRange(3, 1)] integerValue] * 3) + | |
([[civilID substringWithRange:NSMakeRange(4, 1)] integerValue] * 7) + | |
([[civilID substringWithRange:NSMakeRange(5, 1)] integerValue] * 9) + | |
([[civilID substringWithRange:NSMakeRange(6, 1)] integerValue] * 10) + | |
([[civilID substringWithRange:NSMakeRange(7, 1)] integerValue] * 5) + | |
([[civilID substringWithRange:NSMakeRange(8, 1)] integerValue] * 8) + | |
([[civilID substringWithRange:NSMakeRange(9, 1)] integerValue] * 4) + | |
([[civilID substringWithRange:NSMakeRange(10, 1)] integerValue] * 2)) % 11; | |
double checksumValue = [[civilID substringWithRange:NSMakeRange(11, 1)] doubleValue]; | |
if (test == checksumValue) { | |
valid = true; | |
} | |
} | |
return valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift 4
version