3

I found a piece of client side Javascript code to check the validity of an IBAN bank account number here: https://stackoverflow.com/questions/21928083/iban-validation-check

Now I'd like to use this code in SSJS to double check the input server side. I thought I'd take a gamble and just simply copy the entire code and see what happens. At least I didn't get the usual 500 error, however, the function doesn't seem to be returning any output in the function name when I call it.

Would appreciate any pointers on how to make this work!

Here's the function:

function isValidIBANNumber(i) {
        var CODE_LENGTHS = {
            AD: 24, AE: 23, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29,
            CH: 21, CR: 21, CY: 28, CZ: 24, DE: 22, DK: 18, DO: 28, EE: 20, ES: 24,
            FI: 18, FO: 18, FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28, HR: 21,
            HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28,
            LI: 21, LT: 20, LU: 20, LV: 21, MC: 27, MD: 24, ME: 22, MK: 19, MR: 27,
            MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25, QA: 29,
            RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26
        };
        var iban = String(i).toUpperCase().replace(/[^A-Z0-9]/g, ''), // keep only alphanumeric characters
                code = iban.match(/^([A-Z]{2})(\d{2})([A-Z\d]+)$/), // match and capture (1) the country code, (2) the check digits, and (3) the rest
                digits;
        // check syntax and length
        if (!code || iban.length !== CODE_LENGTHS[code[1]]) {
            return false;
        }
        // rearrange country code and check digits, and convert chars to ints
        digits = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, function (letter) {
            return letter.charCodeAt(0) - 55;
        });
        // final check
        return mod97(digits);
    }
    function mod97(string) {
        var checksum = string.slice(0, 2), fragment;
        for (var offset = 2; offset < string.length; offset += 7) {
            fragment = String(checksum) + string.substring(offset, offset + 7);
            checksum = parseInt(fragment, 10) % 97;
        }
        return checksum;
    }

And then I call the function and check if == 1

isValidIBANNumber(input)

if (isValidIBANNumber == 1) {

1 Answer 1

3

Turns out writing out your question helps you solve it.

Instead of

isValidIBANNumber(input)

if (isValidIBANNumber == 1) {

It should've been

if (isValidIBANNumber(input) == 1) {
2
  • Yes, it sometimes does. :) you should mark your own answer as solution. This makes it easier for others to see that "the case is closed" Nov 28, 2019 at 14:20
  • Yes will do but I need to wait 2 days before I can mark my own answer as correct :) Nov 29, 2019 at 11:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .