Created
June 2, 2021 11:17
-
-
Save thanospapazoglou/5d17b55373c35530e0f40f154ad1aa7b to your computer and use it in GitHub Desktop.
Greek Tax Number 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
// | |
// 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