Skip to main content
Participating Frequently
December 1, 2023
Question

String mit einer sehr großen Zahl in ein Zahlenformat umwandeln

  • December 1, 2023
  • 1 reply
  • 1695 views

Hallo!

Ich habe in einem Formular ein IBAN Feld, indem ich die Eingabe auch auf korrektheit prüfe.

Mein Code ist fertig und funktioniert bis zur Umwandlung des fertig hergerichteten IBANs (erste 4 stellen nach hinten, alle Buchstaben in die richtigen Zahlen umgewandelt) von einem String zu einer Zahl.

Nach der Umwandlung muss ich schauen, ob der Wert Modulo 97 wirklich 1 ist, ich brauche daher eine Zahl.

Kann mir hier bitte jemand weiterhelfen?

if(event.value) {
    var iban = event.value;
    iban = iban.toUpperCase();
    var str = iban.substring(0,2);

    // Länge für den Ländercode prüfen
    var land = new Array("NO", "BE", "DK", "FO", "FI", "GL", "NL", "SI", "EE", "LT", "LU", "AT", "HR", "LV", "LI", "CH", "BG", "DE", "GB", "IE", "GI", "RO", "SE", "SK", "ES", "CZ", "PT", "IS", "FR", "GR", "IT", "MC", "SM", "PL", "HU", "CY", "MT");

    var len = new Array(15, 16, 18, 18, 18, 18, 18, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 24, 24, 24, 24, 24, 25, 26, 27, 27, 27, 27, 27, 28, 28, 28, 31);

    var j = land.indexOf(str, 0);
    if(j === -1){
        app.alert("Kein gültiger IBAN mit SEPA eingegeben!");
        this.resetForm("IBAN");
    } else if(iban.length != len[j]) {
        app.alert("IBANs mit dem Ländercode " + str + " haben " + len[j] + " stellen!");
        this.resetForm("IBAN");
    }  
  
     // Für die Umwandlung der Buchstaben in Zahlen
    var charCode = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
    var intCode = new Array(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);

    // Ländercode und Prüfsumme nach hinten
    var check = iban.substr(0,4);
    var rest = iban.substr(4);
    var ibanCheck = rest + check;

    // Validieren
    var dividend = "";
    var b = /[A-Z]/;

    for(var i=0; i<ibanCheck.length; i++){
        if(b.test(ibanCheck[i])) {
            j = charCode.indexOf(ibanCheck[i], 0);
            dividend += intCode[j];
        } else {
            dividend += ibanCheck[i];
        }
    }

    dividend = parseFloat(dividend);
app.alert(dividend);
    if (dividend%97 != 1) {
        app.alert("Ungültiger IBAN");
        this.resetForm("IBAN");
    } else { // Für die Ausgabe formatieren
        ibanCheck = "";
        ibanCheck = iban.substring(0,4) + " ";
        for(var i=4; i<iban.length; i++) {
            ibanCheck += iban[i];
            if((i+1)%4 == 0){
                ibanCheck += " ";
            }
        }
    }
    event.value = ibanCheck;
}

  Vielen Dank!

Liebe Grüße,

Conny

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
December 1, 2023

This line does that:

 

if (dividend%97 != 1) {
Participating Frequently
December 2, 2023

So you mean, the problem isn't the conversion from the string to a number but the modulo operation?

try67
Community Expert
Community Expert
December 2, 2023

I don't follow what you mean... But yes, this operator returns a number.

You wrote (translated): need to see if the value Modulo 97 is really 1, so I need a number.

The "%" operator will always return a number, not a string.