Skip to content

Instantly share code, notes, and snippets.

@befy
Last active November 30, 2020 10:42
Show Gist options
  • Save befy/91dbdb9239fbf726cc1eaeeb5d9d6151 to your computer and use it in GitHub Desktop.
Save befy/91dbdb9239fbf726cc1eaeeb5d9d6151 to your computer and use it in GitHub Desktop.
func validateID(_ id: String) -> Bool {
let digits = id.map {Int(String($0))} as! [Int]
guard digits.count == 11, digits[0] != 0, digits[9] != 0 else { return false }
let firstValidation = (digits[0] + digits[2] + digits[4] + digits[6] + digits[8]) * 7
let secondValidation = digits[1] + digits[3] + digits[5] + digits[7]
let tenthDigit = (firstValidation - secondValidation) % 10
let eleventhDigit = (digits.reduce(0, +) - digits[10]) % 10
return (digits[9] == tenthDigit && digits[10] == eleventhDigit)
}
//usage
validateID("49673861228") //returns true and generated from https://www.simlict.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment