2 Replies Latest reply on Aug 11, 2014 6:44 AM by ahlaj77

    Limit the amount of characters of a text field based on first digit

    ahlaj77 Level 1

      Hello and thanks in advance for your help!

       

      I would like to limit the amount of characters of the text field based on the first digit of the number (the text field is only limited to a number format...no decimals, no commas).

      For example, if the number begins with a 3, I would like to limit the text field to allow only ten characters. I have three scenarios but if I could get started with some code and what is the best place to add it (keystroke or validation?) I can take it from there. Thanks again for your help!!

        • 1. Re: Limit the amount of characters of a text field based on first digit
          try67 MVP & Adobe Community Professional

          I've written this code for you that does that. Use it as the field's custom Keystroke code:

           

           

          // Validate that only digits are entered
          if (event.change) {
              event.rc = /^\d+$/.test(event.change);
          }
          
          // Validate string length if it starts with 3
          if (/^3/.test(AFMergeChange(event))) {
              event.rc = AFMergeChange(event).length <= 10;
              if (!event.rc) app.alert("If the number starts with \"3\" it may not be longer than 10 digits.",1); // optional error message
          }
          

           

          You can duplicate the second part of it for additional conditions, but keep in mind that this code won't even let you remove the first character in the field if the result is an invalid one.

          For example, if you enter "234567890123456" then you can't remove the "2" at the start because that would result in an invalid number. You can remove any of the other digits, though, and when it's 10 digits or less then you could remove the starting "2" as well.

          • 2. Re: Limit the amount of characters of a text field based on first digit
            ahlaj77 Level 1

            Thanks try67!! That worked like a charm! I appreciate your help!!