Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBAN Validator Swift

Tags:

ios

swift

iban

I am writing an algorithm to validate IBAN (International Bank Account Number) in Swift 3 and not able to figure one of the validation.

Example IBAN - BE68539007547034

Here are the rules to validate -

  1. Input number should be of length 16.
  2. First 2 characters are country code (not numeric).
  3. Last 14 are numeric.
  4. Last 2 characters are the modulo 97 result of the previous 12 numeric characters.

While #1 - #3 are clear I need clarity on #4. If anyone have done this before and know about it then please let me know.

like image 709
Abhinav Avatar asked May 05 '17 11:05

Abhinav


People also ask

How do I check my IBAN validation?

IBAN Checksum This is the first and most important check we perform. The IBAN check digit consists of two digits in positions 3 and 4 of the IBAN. It is calculated using the MOD97 algorithm and provides the primary integrity check for the IBAN standard. Supported for all 116 countries.

Can I find a SWIFT code from an IBAN number?

You will typically be able to find your SWIFT code on bank statements and on your online or app banking. Most often it will be in the same place as your IBAN number.

Why is it saying my IBAN is incorrect?

If the IBAN you're entering is not accepted in Bankline, double check you've entered the information correctly and try again. If it's still not working you should check with the beneficiary that the information is correct.

What is IBAN number SWIFT?

A Society for Worldwide Interbank Financial Telecommunication (SWIFT) code is used to identify a specific bank during an international transaction. An International Bank Account Number (IBAN) is used to identify an individual account involved in the international transaction.


2 Answers

The validation algorithm is rather simple if you follow the algorithm on wikipedia:

extension String {
    private func mod97() -> Int {
        let symbols: [Character] = Array(self)
        let swapped = symbols.dropFirst(4) + symbols.prefix(4)

        let mod: Int = swapped.reduce(0) { (previousMod, char) in
            let value = Int(String(char), radix: 36)! // "0" => 0, "A" => 10, "Z" => 35
            let factor = value < 10 ? 10 : 100          
            return (factor * previousMod + value) % 97
        }

        return mod
    }    

    func passesMod97Check() -> Bool {
        guard self.characters.count >= 4 else {
            return false
        }

        let uppercase = self.uppercased()

        guard uppercase.range(of: "^[0-9A-Z]*$", options: .regularExpression) != nil else {
            return false
        }

        return (uppercase.mod97() == 1)
    }
}

Usage:

let iban = "XX0000000..."
let valid = iban.passesMod97Check()

If you want to validate the format for a specific country, just modify the regular expression, e.g.

"^[A-Z]{2}[0-9]{14}$"

or directly

"^BE\\d{14}$"
like image 112
Sulthan Avatar answered Nov 04 '22 18:11

Sulthan


From Wikipedia

let IBAN = "GB82WEST12345698765432" // uppercase, no whitespace !!!!
var a = IBAN.utf8.map{ $0 }
while a.count < 4 {
    a.append(0)
}
let b = a[4..<a.count] + a[0..<4]
let c = b.reduce(0) { (r, u) -> Int in
    let i = Int(u)
    return i > 64 ? (100 * r + i - 55) % 97: (10 * r + i - 48) % 97
}
print( "IBAN \(IBAN) is", c == 1 ? "valid": "invalid")

prints

IBAN GB82WEST12345698765432 is valid

With IBAN from your question it prints

IBAN BE68539007547034 is valid
like image 24
user3441734 Avatar answered Nov 04 '22 17:11

user3441734