Created
June 2, 2021 18:33
-
-
Save thanospapazoglou/d8d39249c32a86e83b81f7f5ba28b310 to your computer and use it in GitHub Desktop.
Greek Social Security Number (SSN) Validation in Swift - Έλεγχος ορθότητας ΑΜΚΑ σε Swift
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
// | |
// GreekSSNValidation.swift | |
// | |
// Created by Athanasios Papazoglou. | |
// | |
// Any feedback is appreciated 🤜🤛. | |
// Also, you can reach me on Twitter (at)A_Ch_Papazoglou | |
import Foundation | |
extension String { | |
var isValidGreekSSNNumber: Bool { | |
if (self.range(of: "^\\d{11}$", options: .regularExpression) != nil) && (self != "00000000000") { | |
var sum = 0 | |
let intArraySSN = self.compactMap({ Int(String($0)) }) | |
for i in stride(from: 1, through: self.count, by: 1) { | |
var ithDigit = intArraySSN[i-1] | |
if (i % 2 == 0) { | |
ithDigit *= 2 | |
if (ithDigit > 9) { | |
ithDigit -= 9 | |
} | |
} | |
sum += ithDigit | |
} | |
return sum % 10 == 0 | |
} else { | |
return false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment