8 Replies Latest reply on Oct 29, 2012 11:17 AM by gkaiseril

    Different formats of the same numerical field

    mm1978

      Hello,

       

      I am trying to do the following:

       

      Field 01 is the source where I will inpute a numerical value: e.g. 123456789

      Field 02 should format the value from Field 01 into: 123-456-789

      Field 03 should format the value from Filed 01 into: 12-23-45-78-9

       

      What script to write and how to set up Field 02 and Field 03.

       

      I greatly appreciate your help.

       

      Thank you.

        • 1. Re: Different formats of the same numerical field
          try67 MVP & Adobe Community Professional

          You'll need to better describe the rules to generate the values of fields 2

          and 3.

          Is the value in field 1 always going to be 9 digits? If not, how do you

          want to split it when copying it over to those fields?

          • 2. Re: Different formats of the same numerical field
            mm1978 Level 1

            The value in Field 1 will always have the same amount of digits. I will enter it once and I would like Field 2 and Field 3 to format the same number from Field 1 differently, i.e. Field 2: 999-999-999 and Field 3: 99-99-99-99-9.

             

            Thanks

            • 3. Re: Different formats of the same numerical field
              try67 MVP & Adobe Community Professional

              Field2 custom calculation script:

              var v1 = this.getField("Field1").valueAsString;

              event.value = v1.substring(0,3) + "-" + v1.substring(3,6) + "-" +

              v1.substring(6)

               

              Field3 custom calculation script:

              var v1 = this.getField("Field1").valueAsString;

              event.value = v1.substring(0,2) + "-" + v1.substring(2,4) + "-" +

              v1.substring(4,6) + "-" + v1.substring(6,8) + "-" + v1.substring(8)

              • 4. Re: Different formats of the same numerical field
                mm1978 Level 1

                Thank you. The formating works; however, it only works if Field 2 and Field 3 are blank. Can it be updated everytime I change Field 1.

                The reason is that if I make a mistake and enter the wrong number into Field 1, then when I correct Field 1, Field 2 and Field 3 should update automatically.

                 

                Is it possible?

                 

                Thank you.

                • 5. Re: Different formats of the same numerical field
                  try67 MVP & Adobe Community Professional

                  It should work that way... Did you set these scripts as the custom *calculation

                  *scripts, as instructed?

                  • 6. Re: Different formats of the same numerical field
                    mm1978 Level 1

                    Calculation script..... - it works now!!!.

                    I really appreciate your help.

                    Thank you.

                    • 7. Re: Different formats of the same numerical field
                      gkaiseril MVP & Adobe Community Professional

                      Another option is to use the RegExp object to split the string and then add the formatting as needed:

                       

                      // custom calculation script Field01 or custom validation script Field01

                      // field is cusotm arbitrary "999999999'
                      // group in 3 groups of 3 digits
                      this.getField("Field02").value = "";
                      // define RegExp string for format 3 groups of 3 digits add test value
                      if(/^(\d{3})(\d{3})(\d{3})$/.test(event.value) == true) {
                         // add "-" as needed
                         this.getField("Field02").value = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3;
                      }

                      // group in 4 groups of 2 and 1 group of 1
                      this.getField("Field03").value = "";
                      // define RegExp string for format 4 groups of 2 digits and 1 digit add test value
                      if(/^(\d{2})(\d{2})(\d{2})(\d{2})(\d{1})$/.test(event.value) == true) {
                         // add "-" as needed
                         this.getField("Field03").value = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3 + "-" + RegExp.$4 + "-" + RegExp.$5;
                      }

                      • 8. Re: Different formats of the same numerical field
                        gkaiseril MVP & Adobe Community Professional

                        You can even use the util.printx method:

                         

                        // custom calculation script Field01 or custom validation script Field01

                        // using the util.printx metnod

                        // field is cusotm arbitrary "999999999'

                        this.getField("Field02").value = "";

                        this.getField("Field03").value = "";

                        // if field not empty.

                        if(event.vaue != "') {

                        // group in 3 groups of 3 digits

                        this.getField("Field02").value = util.printx("999-999-999", event.value);

                        // group in 4 groups of 2 and 1 group of 1

                        this.getField("Field03").value = util.printx("99-99-99-99-9", event.value);

                        }