Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
14

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

Community Beginner ,
Dec 01, 2023 Dec 01, 2023

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

TOPICS
JavaScript , PDF , PDF forms
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 01, 2023 Dec 01, 2023

This line does that:

 

if (dividend%97 != 1) {
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 02, 2023 Dec 02, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2023 Dec 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 02, 2023 Dec 02, 2023

I know that the modulo operator returns a number. The validation of an IBAN ist that after you moved the first four Symbols (two letters and two numbers) to the end of the IBAN you convert the letters to numbers and then you look, if the created number modulo 97 returns 1. 
So i've generated this number in a string because it was easier, now I need to turn this string into a number format so i can use the modulo operator. I've got problems with the conversion. An austrian IBAN has 20 signs, after conversion of the letters to numbers, it has 22 numbers. the conversion always cuts the last 6 numbers  and so the check doesn't work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 02, 2023 Dec 02, 2023

But I think it's the conversion. I've tested it with the output. The string shows the right 22 numbers. After the conversion, the divident looses the last 6 numbers, so I think its too big and I need another way for conversion.
I've also tried

       BigInt(dividend);

but there was the same error.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2023 Dec 02, 2023

First of all, the value is an Integer, not a Float, so there's no need to use the parseFloat method.

And since it's too big for JS to handle you can perform a piece-wise validation of it.

See: https://en.wikipedia.org/wiki/International_Bank_Account_Number#Modulo_operation_on_IBAN

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 02, 2023 Dec 02, 2023
LATEST

I knew that I woult need an integer,  thats why I've tried the bigInt, but it hasn't worked and so I've tried the next method...

Thanks for the link, it looks like that my answer is in this text!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines