aboutsummaryrefslogtreecommitdiff
path: root/Info/11/2/Codierung_Pruefziffern/iban.html
blob: 1cc36dad8261f466abca400628dfa5f5085b6b0b (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
  <title>IBAN Check</title>
  <meta charset="utf8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="Florian Fischer">

  <style>
    :root {
      /* define applied colors */
      --bg: #fdfdfd;
      --fg: #1a1a1a;
      --link-color: #0000EE;
    }
    @media (prefers-color-scheme: dark) {
      /* change apllied colors to the dark gruvbox pallet
       * https://github.com/morhetz/gruvbox
       */
      :root {
        --bg: #282828; /* dark bg */
        --fg: #ebdbb2; /* dark fg */
	--link-color: #d79921; /* yellow */
      }
    }
    
    html {
      line-height: 1.5;
      font-family: Georgia, serif;
      font-size: 20px;
      color: var(--fg);
      background-color: var(--bg);
    }
    
    body {
      margin: 0 auto;
      max-width: 36em;
      padding-left: 50px;
      padding-right: 50px;
      padding-top: 50px;
      padding-bottom: 50px;
      hyphens: auto;
      word-wrap: break-word;
      text-rendering: optimizeLegibility;
      font-kerning: normal;
    }

    footer {
      position:absolute;
      bottom: 0;
    }

    a {
      color: var(--link-color);
    }

    a:visited {
      color: var(--link-color);
    }
    
    @media (max-width: 600px) {
      body {
        font-size: 0.9em;
        padding: 1em;
      }
    }
  </style>

  <script language="javascript" type="text/javascript">
    /*
    @licstart  The following is the entire license notice for the
    JavaScript code in this page.

    Copyright (C) 2024 Florian Fischer

    The JavaScript code in this page is free software: you can
    redistribute it and/or modify it under the terms of the GNU
    General Public License (GNU GPL) as published by the Free Software
    Foundation, either version 3 of the License, or any later version.
    The code is distributed WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

    As additional permission under GNU GPL version 3 section 7, you
    may distribute non-source (e.g., minimized or compacted) forms of
    that code without the copy of the GNU GPL normally required by
    section 4, provided you include this license notice and a URL
    through which recipients can access the Corresponding Source.

    @licend  The above is the entire license notice
    for the JavaScript code in this page.
    */

    // return IBAN without spaces
    function normalizeIban(ibanInput) {
        return ibanInput.replaceAll(" ", "");
    }

    const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // return True if the IBAN is valid
    function checkIban(ibanInput) {
        try {
            let ibanStr = normalizeIban(ibanInput);
            let countryCode1 = ALPHABET.indexOf(ibanStr.charAt(0).toUpperCase()) + 10;
            let countryCode2 = ALPHABET.indexOf(ibanStr.charAt(1).toUpperCase()) + 10;
            let checkDigits = parseInt(ibanStr.substring(2, 4));
            let bban = ibanStr.substring(4);
            let ibanToCheck = BigInt(bban + countryCode1 + countryCode2 + "00");
            let calculatedCheckDigits = 98 - Number(ibanToCheck % BigInt("97"));
            return checkDigits == calculatedCheckDigits;
        }
        catch (e) {
            return false;
        }
    }

    function visualizeIbanCheck(ibanInput) {
        try {
            let ibanStr = normalizeIban(ibanInput);
            let countryCode1 = ALPHABET.indexOf(ibanStr.charAt(0).toUpperCase()) + 10;
            let countryCode2 = ALPHABET.indexOf(ibanStr.charAt(1).toUpperCase()) + 10;
            let bban = ibanStr.substring(4);
            let ibanToCheck = BigInt(bban + countryCode1 + countryCode2 + "00");
            let calculatedCheckDigits = 98 - Number(ibanToCheck % BigInt("97"));

            document.getElementById('iban_check_visualisation_details').style.display = "initial";

            let vis = document.getElementById('iban_check_visualisation');
            // Clear last visualisation
            vis.innerHTML = "";

            // Add some text to the new cells:
            let equTxt = document.createElement("span");
            equTxt.innerHTML = "98 - (";
            vis.appendChild(equTxt);

            let bbanTxt = document.createElement("span");
            bbanTxt.innerHTML = bban;
            bbanTxt.style.borderStyle="solid";
            bbanTxt.style.borderColor="red";
            vis.appendChild(bbanTxt);

            let ccTxt = document.createElement("span");
            ccTxt.innerHTML = "" + countryCode1 + countryCode2;
            ccTxt.style.borderStyle="solid";
            ccTxt.style.borderColor="orange";
            vis.appendChild(ccTxt);

            let calcTxt = document.createElement("span");
            calcTxt.innerHTML = "00 % 97) = 98 - ";
            vis.appendChild(calcTxt);

            let modRes = Number(ibanToCheck % BigInt("97"));
            let modResTxt = document.createElement("span");
            modResTxt.innerHTML =  "" + modRes;
            vis.appendChild(modResTxt);

            let equEndTxt = document.createElement("span");
            equEndTxt.innerHTML =  " = ";
            vis.appendChild(equEndTxt);

            let cdTxt = document.createElement("span");
            cdTxt.innerHTML = "" + calculatedCheckDigits;
            if (parseInt(ibanStr.substring(2, 4)) == calculatedCheckDigits) {
                cdTxt.style.color="green";
            } else {
                cdTxt.style.color="red";
            }
            vis.appendChild(cdTxt);
        }
        catch (e) {
            return;
        }
    }

    document.addEventListener('DOMContentLoaded', function() {
      let result = document.getElementById("result");

      document.getElementById("iban_check_form").onsubmit = function() {
        let ibanInput = document.getElementById('iban').value;
        if (checkIban(ibanInput)) {
          result.innerHTML = "valid ✔";
        } else {
          result.innerHTML = "invalid ✘";
        }
        visualizeIbanCheck(ibanInput);
        return false;
      }
    });
  </script>
</head>

<body>
<h2>IBAN Check</h2>
<div>
<form id="iban_check_form">
  IBAN:
  <input id="iban" type="text"/>
  <!-- non visual submit input to submit on "Enter" -->               
  <input type="submit" style="display: none" />
  <input id="iban_check_button" value="check" type="submit">
  </code>
</form>
</div>
<div id="result">
</div>
<details id="iban_check_visualisation_details" style="display: none">
<summary>Visualisierung</summary>
<div id="iban_check_visualisation">
</details>
<footer>
<p xmlns:cc="http://creativecommons.org/ns#">Diese Seite von <span property="cc:attributionName">Florian Fischer</span> ist unter folgender Lizenz veröffentlicht: <a href="https://creativecommons.org/licenses/by-sa/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC BY-SA 4.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt=""><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt=""><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/sa.svg?ref=chooser-v1" alt=""></a></p>
</footer>

</body>
</html>