4 Replies Latest reply on Jun 6, 2014 2:40 PM by stealthrs

    Easiest Way to Add Spaces

    stealthrs Level 1

      I've been trying to figure this out for about 30 minutes, but I'm not a programmer by any means.... On one page, I have agent_code which is 5 characters all together... On another page, I need to split 2 of the characters in one box and 3 in another.... Because of the content on the page I have to fit the typed data into, I need to add spaces... So if I typed in X1A43 into the agent_code box... I need it to come out as "X 1" and "A 4 3"

       

      I have a bootleg work around (literally make 5 output boxes and slice the values), but I'm trying to get better at understanding this.

       

       

      How would you add spaces in between each character in agent_code_part1 and agent_code_part2?

       

       

      // Custom Validate script

       

      (function () {

           // Do nothing if field is blank

          if (!event.value) {

              getField("agent_code_part1").value = event.value = "";

              getField("agent_code_part2").value = event.value = "";

          }

       

          // Populate the agent_code_part1 and agent_code_part 2 with the first 2 characters and last 3 characters entered into this field, respectively

          if (event.value.length === 5) {

              getField("agent_code_part1").value = event.value.slice(0,2);

              getField("agent_code_part2").value = event.value.slice(2,5);

          }

      })();

        • 1. Re: Easiest Way to Add Spaces
          gkaiseril MVP & Adobe Community Professional

          If this is a validation script, wouldn't you also want to validate that the agent code is 5 characters long?

           

          // clear agent part fields;

          this.resetForm(["agent_code_part1", "agent_code_part2"])

          // assume valid input;

          event.rc = true;

          // test for a 5 alphanumeric string and spilt the string into 2, 1, 2 pieces;

          if(/^(\w{2})(\w{1})(\w{2})$/.test(event.value)) {

          this.getField("agent_code_part1").value = RegExp.$1

          this.getField("agent_code_part2").value = RegExp.$2 + " " + RegExp.$3;

          } else if(event.value != "") {

          event.rc = false;

          app.alert("Agent code needs to be 5 characters long", 0, 0);

          }

           

           


          • 2. Re: Easiest Way to Add Spaces
            stealthrs Level 1

            It is definitely helpful, thanks.

             

            So... I don't exactly know what you did, but I tried my best to figure it out... To get it to do exactly what I wanted to do (I needed an extra two spaces), I changed the code to this, and it worked like a charm... Thanks!

             

            // clear agent part fields;

            this.resetForm(["agent_code_part1", "agent_code_part2"])

             

            // assume valid input;

            event.rc = true;

             

            // test for a 5 alphanumeric string and spilt the string into 2, 1, 2 pieces;

            if(/^(\w{1})(\w{1})(\w{1})(\w{1})(\w{1})$/.test(event.value))

            {

            this.getField("agent_code_part1").value = RegExp.$1 + " " + RegExp.$2

            this.getField("agent_code_part2").value = RegExp.$3 + " " + RegExp.$4 + " " + RegExp.$5;

            }

             

            else if(event.value != "") {

            event.rc = false;

            app.alert("Agent code needs to be 5 characters long", 0, 0);

            }

            • 3. Re: Easiest Way to Add Spaces
              stealthrs Level 1

              P.S. Is it just me, or did Adobe's forums lose a ton of functionality and become a lot more difficult to navigate and search through archives????

               

              I can't even edit previous posts now

               

              :update: Figured out how to edit posts, but this site is a lot more disorganized (functionally speaking)

              • 4. Re: Easiest Way to Add Spaces
                stealthrs Level 1

                GK,

                 

                What if I needed add spacing like before, but only pulling from 1 field that doesn't have a length requirement... The original field with no spaces can be at minimum 1 characters.  Let's say it's called percentage_case1

                 

                The output box (percentage_case1_juv) is what needs the spaces between the characters... So if I typed in 100 into percentage_case1, then percentage_case1_juv would read "1 0 0"

                 

                If I typed in So if I typed in 90 into percentage_case1, then percentage_case1_juv would read "9 0"