Skip to content

Commit

Permalink
Added frontend validation to check if given IBAN is valid
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
MasterZydra committed Feb 29, 2024
1 parent d5a0f24 commit f4a1758
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Types of changes: `Added`, `Changed`, `Deprecate`, `Removed`, `Fixed`, `Secruity

### Added
- Added subdistrict recommendations to the create and edit form for plot
- Added frontend validation to check if given IBAN is valid

## v2.2.1 - 27.02.2024 - Added developer setting

Expand Down
35 changes: 35 additions & 0 deletions public/js/validateIBAN.js
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;
}
2 changes: 2 additions & 0 deletions resources/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
'German' => 'Deutsch',
'Home' => 'Startseite',
'IBAN' => 'IBAN',
'IbanIsNotValid' => 'IBAN ist nicht gültig',
'IbanIsValid' => 'IBAN ist gültig',
'Imprint' => 'Impressum',
'InsertSearchText' => 'Suchtext eingeben...',
'InvalidDataTypeForField' => 'Ungültiger Datentyp für das Feld "%s"',
Expand Down
2 changes: 2 additions & 0 deletions resources/Lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
'German' => 'German',
'Home' => 'Home',
'IBAN' => 'IBAN',
'IbanIsNotValid' => 'IBAN is not valid',
'IbanIsValid' => 'IBAN is valid',
'Imprint' => 'Imprint',
'InsertSearchText' => 'Insert search text...',
'InvalidDataTypeForField' => 'Invalid data type for field "%s"',
Expand Down
2 changes: 1 addition & 1 deletion resources/Views/about.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<table class="scrollable">
<tr>
<td>Bio-Manager Version</td>
<td class="right">2.2.1</td>
<td class="right">2.2.2</td>
</tr>
<tr>
<td><?= __('Developer') ?></td>
Expand Down
23 changes: 22 additions & 1 deletion resources/Views/settings/editInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<input id="invoiceBankName" name="invoiceBankName" type="text" value="<?= setting('invoiceBankName') ?>" required><br>

<label for="invoiceIBAN" class="required"><?= __('IBAN') ?>:</label><br>
<input id="invoiceIBAN" name="invoiceIBAN" type="text" value="<?= setting('invoiceIBAN') ?>" required><br>
<input id="invoiceIBAN" name="invoiceIBAN" type="text" value="<?= setting('invoiceIBAN') ?>" onchange="validateIbanInput()" required><br>
<div style="margin-bottom: 8px;"><span id="ibanValidMsg"></span></div>

<label for="invoiceBIC" class="required"><?= __('BIC') ?>:</label><br>
<input id="invoiceBIC" name="invoiceBIC" type="text" value="<?= setting('invoiceBIC') ?>" required><br>
Expand All @@ -42,4 +43,24 @@
<button><?= __('Save') ?></button>
</form>

<script src="js/validateIBAN.js"></script>
<script>

function validateIbanInput()
{
let msgSpan = document.getElementById('ibanValidMsg');
if (validateIBAN(document.getElementById('invoiceIBAN').value)) {
console.log("yes");
msgSpan.style = 'color: #4a8a16;';
msgSpan.textContent = '<?= __('IbanIsValid') ?>';
} else {
console.log("no");
msgSpan.style = 'color: #b01c1c;';
msgSpan.textContent = '<?= __('IbanIsNotValid') ?>';
}
}

validateIbanInput();
</script>

<?= component('layout.footer') ?>

0 comments on commit f4a1758

Please sign in to comment.