Skip to content

Instantly share code, notes, and snippets.

@thanospapazoglou
Created June 2, 2021 11:17
Show Gist options
  • Save thanospapazoglou/5d17b55373c35530e0f40f154ad1aa7b to your computer and use it in GitHub Desktop.
Save thanospapazoglou/5d17b55373c35530e0f40f154ad1aa7b to your computer and use it in GitHub Desktop.
Greek Tax Number Validation in Swift - Έλεγχος ορθότητας ΑΦΜ σε Swift
//
// GreekTaxValidation.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 isValidGreekTAXNumber: Bool {
if (self.range(of: "^\\d{9}$", options: .regularExpression) != nil) && (self != "000000000") {
var sum = 0
let intArrayTax = self.compactMap({ Int(String($0)) })
for i in stride(from: 7, through: 0, by: -1) {
sum += intArrayTax[i] * Int(pow(Double(2), Double(8-i)))
}
return sum % 11 % 10 == intArrayTax[8]
} else {
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment