IBAN szám ellenőrzése C# -ban
You are Reading..
IBAN szám ellenőrzése C# -ban
Tegnap áttettem Delphi -ből C# -ba a nemzetközi bankszámlaszám (IBAN – International Bank Account Number) ellenőrző algoritmust. Előnye, hogy teljesen országfüggetlen. Teszteléshez pár minta:
LB62099900000001001901229114
HU42117730161111101800000000
SI56191000000123438
És a kód:
public class BankAccountCheck { private string ChangeAlpha(string input) { string result = input; for (int i = 65; i <= 90; i++) { result = result.Replace(Convert.ToChar(i).ToString(), Convert.ToString(i - 55)); } return result; } private int CalculateDigits(string iban) { iban = iban.ToUpper(); if (iban.IndexOf("IBAN") > -1) iban = iban.Replace("IBAN", ""); iban += iban.Substring(0, 4); iban = iban.Remove(0, 4); iban = ChangeAlpha(iban); int v = 1; int l = 9; int rest = 0; int number = 0; string alpha = ""; try { do { if (l > iban.Length) l = iban.Length; alpha = alpha + iban.Substring(v - 1, ((v - 1) + l) > iban.Length ? (iban.Length - (v - 1)) : l); number = Convert.ToInt32(alpha); rest = number % 97; v += l; alpha = rest.ToString(); l = 9 - alpha.Length; } while (v <= iban.Length); } catch { rest = 0; } return rest; } public bool CheckIBAN(string iban, bool justcountrycode = false) { string[] countrycodes = {"AD","AE","AL","AT","BA","BE","BG","CH","CY","CZ","DE","DK", "EE","ES","FI","FO","FR","GB","GE","GI","GL","GR","HR","HU","IE","IL","IS", "IT","KW","KZ","LB","LI","LT","LU","LV","MC","ME","MK","MR","MT","MU","NL", "NO","PL","PT","RO","RS","SA","SE","SI","SK","SM","TN","TR"}; bool ret = true; iban = iban.Replace(" ", "").Replace("-",""); if (Array.IndexOf(countrycodes,iban.Substring(0,2))< 0) { ret = false; } if (justcountrycode) return ret; if (CalculateDigits(iban) == 1) ret = true; else ret = false; return ret; } }