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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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; } } |