Skip to content

Instantly share code, notes, and snippets.

@thanospapazoglou
Created June 2, 2021 18:33
Show Gist options
  • Save thanospapazoglou/d8d39249c32a86e83b81f7f5ba28b310 to your computer and use it in GitHub Desktop.
Save thanospapazoglou/d8d39249c32a86e83b81f7f5ba28b310 to your computer and use it in GitHub Desktop.
Greek Social Security Number (SSN) Validation in Swift - Έλεγχος ορθότητας ΑΜΚΑ σε Swift
//
// 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