Skip to content

Instantly share code, notes, and snippets.

@didats
Last active December 4, 2018 15:57
Show Gist options
  • Save didats/7521151 to your computer and use it in GitHub Desktop.
Save didats/7521151 to your computer and use it in GitHub Desktop.
Civil ID Validation for Kuwait Citizen in Objective C
-(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;
}
@hsharghi
Copy link

hsharghi commented Dec 4, 2018

Swift 4 version

  func validateCivilID(civilID: String) -> Bool {
        
        func digitWeight(string: String, offset: Int, weight: Int = 1) -> Int {
            let fromIndex = string.index(string.startIndex, offsetBy: offset)
            let toIndex = string.index(fromIndex, offsetBy: 1)
            let digit = string[fromIndex..<toIndex]
            if let value = Int(digit) {
                return value * weight
            }
            
            return 0
        }
        
            var valid = false
            let regex = "[0-9]{12}"
        
        let formatTest = NSPredicate(format: "SELF MATCHES %@", regex)
        if formatTest.evaluate(with: civilID) {
            
            let sum =
            digitWeight(string: civilID, offset: 0, weight: 2) +
            digitWeight(string: civilID, offset: 1, weight: 1) +
            digitWeight(string: civilID, offset: 2, weight: 6) +
            digitWeight(string: civilID, offset: 3, weight: 3) +
            digitWeight(string: civilID, offset: 4, weight: 7) +
            digitWeight(string: civilID, offset: 5, weight: 9) +
            digitWeight(string: civilID, offset: 6, weight: 10) +
            digitWeight(string: civilID, offset: 7, weight: 5) +
            digitWeight(string: civilID, offset: 8, weight: 8) +
            digitWeight(string: civilID, offset: 9, weight: 4) +
            digitWeight(string: civilID, offset: 10, weight: 2)
            
            let validityTest = 11 - sum % 11
            let checksum = digitWeight(string: civilID, offset: 11)
            if validityTest == checksum {
                valid = true
            }
            
        }
        
        return valid
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment