how do i limit user choice to three options?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NellievD
    New Member
    • Oct 2014
    • 14

    how do i limit user choice to three options?

    I have a field that allows entries of 1 to 100, or the value "r" or the value "i" or the value "na"

    My script looks like this, but it continues to allow duplicate entries of the "i" and the "r" when the
    "i" is entered on the change* event. What is missing?

    if (xfa.event.chan ge!="" && !xfa.event.newT ext.match(/^[0-9]+(\+|-)?$|^(na?)?$|^( r)?$|^(i)/)) {
    xfa.event.chang e ="";
    xfa.host.beep(" 3");
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't know what it is you're trying to do. Your regex makes no sense.

    Comment

    • NellievD
      New Member
      • Oct 2014
      • 14

      #3
      I'm working in livecycle designer es4. I want to limit the entry to the following options: any numerals 1-100; and then three entries only: the value "na", the value "r" or the value "i".
      This is on the change event. Everything works except that the "i" value also accepts "ir"

      This is the script that works with the exception as noted above:

      if (xfa.event.chan ge!="" && !xfa.event.newT ext.match(/^[0-9]+?$|^(na?)?$|^( r)?$|^(i)?$/)) {
      xfa.event.chang e ="";
      xfa.host.messag eBox("Oops, incorrect entry, or caps lock are on. Thank you.");
      }

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That looks convoluted, try this: /^[0-9]+(na|r|i)$/

        Comment

        • NellievD
          New Member
          • Oct 2014
          • 14

          #5
          When i change the script as per above, I can get the numerals, but no other entries. Only numerals are to be allowed, OR any of the three options, but not both.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Works fine for me in a regex tester. Please post your newly updated code.

            Comment

            • NellievD
              New Member
              • Oct 2014
              • 14

              #7
              Here is the revised script I am trying on your suggestion:

              On change*
              // Test input in real time. Only allow approved grades, r, i, na
              if (xfa.event.chan ge!="" && !xfa.event.newT ext.match (/^[0-9]+(na|r|i)$/)) {
              xfa.event.chang e ="";
              xfa.host.messag eBox("Oops, incorrect entry, or caps lock are on. Thank you.");
              }

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Don't know what could be wrong. I tested it in a regex tester and it matched: "1na", "949r", "838838i". But did not match "1", "na", "iiiii".

                One thing you can test is to output the value to make sure it's testing the correct input string.

                Comment

                • NellievD
                  New Member
                  • Oct 2014
                  • 14

                  #9
                  Thanks for continuing to stick with me! Output values should be restricted to:
                  either a number
                  OR
                  the value of "r"
                  OR
                  the value of "i"
                  OR
                  the value of "na"

                  So no combined values, just the very specific options as above. Do you mind having another look?

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    That's not what you said in post #3... But no matter, it's a simple fix, include the digits in the parentheses and add a pipe character.

                    Comment

                    • NellievD
                      New Member
                      • Oct 2014
                      • 14

                      #11
                      Sorry about that ... would you mind sending along the code as you suggested? I know I'm missing something here...

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        Fairly simple change to my original code
                        /^([0-9]+|na|r|i)$/

                        Comment

                        • NellievD
                          New Member
                          • Oct 2014
                          • 14

                          #13
                          Thank you ... I've tried the code and I can make entries for any number values that are three digits, or I can enter the value "r" or I can enter the value "i".

                          These are all good, but now I can't successfully enter the value "na". Do you have a suggestion for how i can do this successfully, and also without running into the problem where either the "n" or "a" values can be entered separately without throwing an error?

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            You should be aware that it doesn't limit to 3 digits. You can have any number of digits, if you need only 3, then you need to put that into the expression.

                            The reason you can't enter na is because you are calling the code on change. That means as soon as you enter n it calls the code and it throws an error. You need to use a different event. Like an after update event or a lost focus event. One that only fires after they have finished entering data in that field.

                            You shouldn't be able to have an "n" or "a" by itself as a valid value. Those values aren't among your list of acceptable values. If you are changing what is acceptable, then you can just add them to the expression like any of the other values, within the parenthses separated from the other values with a pipe character.

                            Comment

                            • NellievD
                              New Member
                              • Oct 2014
                              • 14

                              #15
                              The field length is limited to a maximum of three characters, so that isn't a problem. But I see what you mean with entering n on change using your script. Could you resend your script to show that the digits are limited to only the values of 0 to 100 (like grades on a report card?) I'll keep at it ...

                              Comment

                              Working...