Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Singapore NRIC Validation
- (BOOL)validateNRIC:(NSString*)nric {
nric = [nric uppercaseString];
// NRIC length should be 9
if ([nric length]!=9) {
return NO;
}
//NRIC.charAt( 0 ) should be “S” or “T”
unichar prefix = [nric characterAtIndex:0];
if ((prefix != 'S') && (prefix != 'T')) {
return NO;
}
//int number = NRIC.substring( 1, 8 )
//If “number” is not numeric then False
NSString *number = [nric substringWithRange:NSMakeRange(1, 7)];
NSMutableCharacterSet *numbers = [NSCharacterSet decimalDigitCharacterSet];
if ([number rangeOfCharacterFromSet:[numbers invertedSet]].location != NSNotFound) {
return NO;
}
NSInteger sum = 0;
sum += ([number characterAtIndex:0]-48) * 2;
sum += ([number characterAtIndex:1]-48) * 7;
sum += ([number characterAtIndex:2]-48) * 6;
sum += ([number characterAtIndex:3]-48) * 5;
sum += ([number characterAtIndex:4]-48) * 4;
sum += ([number characterAtIndex:5]-48) * 3;
sum += ([number characterAtIndex:6]-48) * 2;
if (prefix=='T') {
sum += 4;
}
NSInteger p = 11-sum%11;
unichar suffix = [nric characterAtIndex:8];
switch (p) {
case 1:
if (suffix == 'A')
return YES;
break;
case 2:
if (suffix == 'B')
return YES;
break;
case 3:
if (suffix == 'C')
return YES;
break;
case 4:
if (suffix == 'D')
return YES;
break;
case 5:
if (suffix == 'E')
return YES;
break;
case 6:
if (suffix == 'F')
return YES;
break;
case 7:
if (suffix == 'G')
return YES;
break;
case 8:
if (suffix == 'H')
return YES;
break;
case 9:
if (suffix == 'I')
return YES;
break;
case 10:
if (suffix == 'Z')
return YES;
break;
case 11:
if (suffix == 'J')
return YES;
break;
default:
break;
}
return NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment