Print Page | Close Window

Senior needs help with JavaScript

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=2234
Printed Date: 15 Dec 2017 at 1:58pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Senior needs help with JavaScript
Posted By: CalamityJane
Subject: Senior needs help with JavaScript
Date Posted: 10 Sep 2017 at 8:51pm
Hi folks!

here's a piece of code that I'm trying to get it run in a acrobat form. When clicked on a button (called "check iban" - brilliant!) it checks if the iban is valid or not.

First part, code checks, if IBAN has a valid length. Until here the code works.
Second part checks the iban with a modulo calculation. For that, the IBAN string must be reorganised so that it's length can be up to around 40 digits  - too big for modulo.
So for modulo it is necessary to calc in steps.

If I click the check-button, nothing happens - as if not clicked. Somewhere must be a bug and I can not find it. Need your help! Cry


var ibannumstr = this.getfield("IBAN").valueasstring;
var ibanswap = "";
retwert = 0;

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 = ibannumstr.toUpperCase().replace(/[^A-Z0-9]/g, '');
var code = iban.match(/^([A-Z]{2})(\d{2})([A-Z\d]+)$/);
var digits = "";

if (!code || iban.length !== CODE_LENGTHS[code[1]]) {
   retwert = 0;
} else {

// ****** Code works well until "else" . Here starts the trouble somewhere ****** //

   ibanswap = ibannumstr.substr(4) + ibannumstr.substr(0,4);
   // takes the first 4 chars and put them to the end of the string - no change in string length allowed //



   var i = 0;
   var n = 0;
   for (i = 0; i <= ibanswap.length; i++) {
      if (ibanswap.charAt(i).toUppercase() != ibanswap.charAt(i).toLowercase()) {
      n = ibanswap.charcodeat(i) - 55;
      digits += n.toString();
      } else {
      digits += ibanswap.charAt(i);
      }
   }
   // checking string char by char if a char is a letter or a number.//
   // If letter, get the ascii-code, subtract 55, add it as a string to the digits-string //
   // If number, add it as a string to the digits-string //



   while (digits.length > 8) {
      n = parsefloat(digits.substr(0,4)) % 97;
      digits = digits.substr.(4);
      if (n > 0 ) {
         digits = n.toString() + digits;
      }
   }
   // digits can be up to 50 chars long. To get a modulo operation, we have to do it in steps.//
   // step 1: get the first 4 chars, turn it into a number, calculate modulo //
   // step 2: remove the first 4 chars in digits-string //
   // step 3: if modulo-calc returns <> 0, put the result as a string in front of the digits-string //
   // goto step 1 until length is < 8 // 


   n = parsefloat(digits) % 97;
   if (n = 1){
      retwert = 1;
   } else {
      retwert = 0;
   }
  // modulo-calc with the rest of the string which can be up to 8 chars //
  // if result is 1, iban number is valid, if <> 1, not valid //
}

source:
code from here: /stackoverflow. com/questions/21928083/iban-validation-check
changes made to get it run in Acrobat - not yet successful



-------------
The End is near



Replies:
Posted By: BAlheit
Date Posted: 11 Sep 2017 at 8:26am
Use:
   if (n == 1){
      retwert = 1;
   } else {
      retwert = 0;
   }

not:
   if (n = 1){
      retwert = 1;
   } else {
      retwert = 0;
   }


Posted By: CalamityJane
Date Posted: 11 Sep 2017 at 6:38pm
Hi BAlheit,

many thanks but this was not the "big" bug. I changed to code but it doesn't get better with it. It's still nochtinh happend when clicked.

By the way. That button checks a lot of other things, too. For example if strings exist for name and surname, address, phone number, bank details, and so on.

Is there a maximum length for a code that can be run within a acrobat button?

 


-------------
The End is near


Posted By: try67
Date Posted: 26 Sep 2017 at 12:44pm
Where did you place the code? How are you executing it?
It doesn't seem to do anything with the result... You should at least add a console.println or app.alert call to show the final value... Otherwise it's normal that nothing happens when you run it.

And no, there's no limit to the amount of code that can be used, but there is a limit to the length of the code that can be entered into the built-in JS editor, but you're no where near it with the code above, and when you reach it there will be a clear error message saying you need to use an external editor.


-------------
- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net