-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added frontend validation to check if given IBAN is valid
Fixes #50
- Loading branch information
1 parent
d5a0f24
commit f4a1758
Showing
6 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Check if the given IBAN is valid. It only supports support for German IBANs | ||
* | ||
* Calculation steps from https://www.hettwer-beratung.de/sepa-spezialwissen/sepa-kontoverbindungsdaten/iban-pr%C3%BCfziffer-berechnung/ | ||
*/ | ||
function validateIBAN(iban) | ||
{ | ||
// A = 10, B = 11, ... | ||
function charToNum(char) | ||
{ | ||
return char - 64 + 9 | ||
} | ||
|
||
// Enforce uppercase | ||
iban = iban.toUpperCase(); | ||
|
||
// Remove leading and trailing spaces as well as all spaces inside | ||
iban = iban.trim().replaceAll(' ', ''); | ||
|
||
// Check length | ||
if (iban.length !== 22) { | ||
return false; | ||
} | ||
|
||
// BBAN = "Bank code" + "Account number" | ||
let bban = iban.slice(4, 22); | ||
// e.g. DE = 131400 | ||
let countryCode = charToNum(iban.charCodeAt(0)).toString() + charToNum(iban.charCodeAt(1)) + "00"; | ||
let checkNumber = bban.toString() + countryCode.toString(); | ||
let checkSum = Number(BigInt(checkNumber) % BigInt(97)); | ||
console.log("checkSum", checkSum); | ||
let ibanCheckSum = (98 - checkSum).toString().padStart(2, '0'); | ||
|
||
return iban.slice(2, 4) === ibanCheckSum; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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