4 Replies Latest reply on Aug 7, 2011 11:19 PM by maxwyss

    getting individual characters from a text box.

    MikeABca

      Another beginner question.  I am writing a script that checks that the vin num (serial num) of a vehicle is correct.  I need to access each of the 17 character entered into a textbox.  I would like to be able to count the char enter (right now it is limited to 17) and I need to multiply each char by different numbers to check it against the check digit of the vin.  I think my problem is that I can't get value of the field recognized as either a string or a series of char.  I get a syntax error on line 5 when I add  *** :string    *** to the line.  If I don't then it does not recognize the **** .charAt()  **** function in line 6.  Do I need to add a header or somethin? 

       

      Also a side question, is there a way to search prior post in just the Acrobat Scripting discussion?  When I do a search it seems to search all forums and can't find what I want.

       

       

      var theVal = 0;

      var theChar = ' ';

       

      var theVin = this.getField("Vin");

      var theString:string = theVin.value;

      theChar = theString.charAt(0);

      theVal = value(theChar);

       

      theVal = theVal * 8;

      this.getField("checkVin").value = theVal;

        • 1. Re: getting individual characters from a text box.
          gkaiseril MVP & Adobe Community Professional

          Check the syntax of line 5.

           

          "var theString:string = theVin.value;"

           

          The ":" is not a valid character in this statement.

           

          There is no 'value' constrictor.

           

          If you are trying to check if a character is a number, then use the "isNaN" method to detect "Not a Number" values.

           

          You might find a JavaScirpt to perform this task if you google for the code.

           

          I found the following function that could be used.

           

          var vinletters=new Array();
          vinletters[1]="AJ";
          vinletters[2]="BKS";
          vinletters[3]="CLT";
          vinletters[4]="DMU";
          vinletters[5]="ENV";
          vinletters[6]="FW";
          vinletters[7]="GPX";
          vinletters[8]="HY";
          vinletters[9]="RZ";
          //--------------12345678901234567;
          var vinweight= "8765432T098765432";
          function valvin(serie){
          if(serie.length == 17){
          suma = 0;
          for (i=0;i < serie.length; i++) {
          nextchar = serie.charAt(i);
          for (j in vinletters) {
          if(vinletters[j].indexOf(nextchar) != -1){
          nextchar = j;
          }
          }
          val = parseInt(nextchar);
          weight = vinweight.charAt(i);
          if(weight == "T"){
          weight = 10;
          }else{
          weight = parseInt(weight);
          }
          suma += val * weight;
          }
          controlchar = suma % 11;
          if(controlchar == 10) controlchar = "X";
          if(controlchar == serie.charAt(8)){
          return true;
          }else{
          return false;
          }
          }else{
          return false;
          }
          return false;
          }

           

          Then the custom calculation scirpt for the 'checkVin" field could be:

           

          event.value = valvin(this.getField('Vin').value)? "Valid": "Invalid";

          • 2. Re: getting individual characters from a text box.
            maxwyss Level 4

            This can be a little bit tricky, because you have to deal with strings and numbers. JavaScript has a few traps in this respect.

             

            The first thing to change is the line

             

               var theString.string = theVin.value

             

            This line of code does not make much sense in Acrobat JavaScript. You can, however, change it to

             

               var theString = theVin.valueAsString ;

             

            This now should make sure that you have a string in the variable.

             

            Having a string, you can treat it like an array, meaning you can address each character via an index number, corresponding its position. The result is again a string.

             

            Assuming that you have all numbers, you can change the strings back to numbers by multiplying it with 1, and do your necessary calculation.

             

            And that should do it.

             

            Hope this can help.

             

            Max Wyss.

            • 3. Re: getting individual characters from a text box.
              MikeABca Level 1

              Thanks Max.

              That is what I needed I did not know how to change the value to a string.  I now have most of the code complete.  So far everything is working great just need to do the finishing touches when I have time.

               

              Mike

              • 4. Re: getting individual characters from a text box.
                maxwyss Level 4

                You're welcome, Mike.

                 

                Sounds good.

                 

                This ambivalence between string and number in JavaScript can play some serious tricks when you are not cautious.

                 

                Max.