By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

how do i limit user choice to three options?

P: 14
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.change!="" && !xfa.event.newText.match(/^[0-9]+(\+|-)?$|^(na?)?$|^(r)?$|^(i)/)) {
xfa.event.change ="";
xfa.host.beep("3");
}
Oct 14 '14 #1

✓ answered by Rabbit

If it's testing fine then it's correct. But it can be cleaned up a little.
Expand|Select|Wrap|Line Numbers
  1. /^([1-9][0-9]?|100|n|r|i)$/

Share this Question
Share on Google+
26 Replies


Rabbit
Expert Mod 10K+
P: 12,364
I don't know what it is you're trying to do. Your regex makes no sense.
Oct 14 '14 #2

P: 14
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.change!="" && !xfa.event.newText.match(/^[0-9]+?$|^(na?)?$|^(r)?$|^(i)?$/)) {
xfa.event.change ="";
xfa.host.messageBox("Oops, incorrect entry, or caps lock are on. Thank you.");
}
Oct 14 '14 #3

Rabbit
Expert Mod 10K+
P: 12,364
That looks convoluted, try this: /^[0-9]+(na|r|i)$/
Oct 14 '14 #4

P: 14
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.
Oct 14 '14 #5

Rabbit
Expert Mod 10K+
P: 12,364
Works fine for me in a regex tester. Please post your newly updated code.
Oct 14 '14 #6

P: 14
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.change!="" && !xfa.event.newText.match (/^[0-9]+(na|r|i)$/)) {
xfa.event.change ="";
xfa.host.messageBox("Oops, incorrect entry, or caps lock are on. Thank you.");
}
Oct 14 '14 #7

Rabbit
Expert Mod 10K+
P: 12,364
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.
Oct 14 '14 #8

P: 14
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?
Oct 14 '14 #9

Rabbit
Expert Mod 10K+
P: 12,364
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.
Oct 15 '14 #10

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

Rabbit
Expert Mod 10K+
P: 12,364
Fairly simple change to my original code
/^([0-9]+|na|r|i)$/
Oct 15 '14 #12

P: 14
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?
Oct 15 '14 #13

Rabbit
Expert Mod 10K+
P: 12,364
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.
Oct 15 '14 #14

P: 14
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 ...
Oct 15 '14 #15

Rabbit
Expert Mod 10K+
P: 12,364
To limit it to 0-100, change it from any number of digits to any 2 digit number or 100.
Oct 15 '14 #16

P: 14
I have decided to work with this approach for what I need to accomplish:

On change*
// Test input in real time. Only allow approved grades, r, i, n with the code on exit replacing n with na
if (xfa.event.change!="" && !xfa.event.newText.match(/^[0-9]+?$|^(^n?)?$|^(r)?$|^(i)?$/)) {
xfa.event.change ="";
xfa.host.messageBox("Oops, incorrect entry, Please correct your input. Thank you.");
}

Then on exit*
this.rawValue=this.rawValue.replace(/n/g,"NA")
this.rawValue=this.rawValue.toUpperCase()

The user is restricted to entering the three options "r", "i", or "n" where "n" is replaced by "na" on exit and all the values are also uppercased. The digits are limited to 3 by virtue of the field length limit.
I have no idea how to limit the number entries from 001 to 100, so that is unfortunate. I'm still open to some help on this! Thanks for all the input...
Oct 15 '14 #17

Rabbit
Expert Mod 10K+
P: 12,364
001-100? Or 1-100? They're different. If the former, limit the first digit to 0 and 1, and the next 2 digits to any number. If the latter, use the method I described in post #16
Oct 15 '14 #18

P: 14
OK, good idea! But where do I find post #16. I looked, I promise!
Oct 15 '14 #19

Rabbit
Expert Mod 10K+
P: 12,364
This post is 20, look up 4 posts.
Oct 15 '14 #20

P: 14
How does this look? (I'm still getting an error when i input three digits like "560:...)
On change*
//Test input in real time. Only allow approved grades, r, i, na
if (xfa.this.change!="" && !xfa.event.newText.match(/^([1-9]{1,2}[0]?|100+|n|r|i)$/)) {
xfa.event.change ="";
xfa.host.messageBox("Incorrect entry.. also, n outputs NA. Thanks!");
}

On exit*
this.rawValue=this.rawValue.replace(/n/g,"NA")
this.rawValue=this.rawValue.toUpperCase()
Oct 16 '14 #21

P: 14
Hello Rabbit,
I've amended the regex for the number entry:

//Test input in real time. Only allow approved grades, r, i, na
if (xfa.this.change!="" && !xfa.event.newText.match(/^[1-9][0-9]?$|^100|^(n?)?$|^(r)?$|^(i)?$/)){
xfa.event.change ="";
xfa.host.messageBox("Incorrect entry.. also, n outputs NA. Thanks!");
}

On exit*
this.rawValue=this.rawValue.replace(/n/g,"NA")
this.rawValue=this.rawValue.toUpperCase()

I think this time I've got it...
Oct 16 '14 #22

Rabbit
Expert Mod 10K+
P: 12,364
Your first attempt is closer, your second one has a bunch of extra stuff for some reason, even on stuff that was working fine before.

Take the first attempt and fix that one. For example, your piece with the 100, you have a plus sign in there for some reason, that tells the expression to match as many zeroes as possible.

And for the piece that defines any two digit number, for some reason you tell it can have an optional zero at the end.
Oct 16 '14 #23

P: 14
You're right: messy code! How about this? It tests fine inside Livecycle ...

On change*

if (xfa.this.change!="" && !xfa.event.newText.match(/^[1-9][0-9]?$|^100|^(n|r|i)$/)){
xfa.event.change ="";
xfa.host.messageBox("Incorrect entry.. also, n outputs NA. Thanks!");
}

On exit*
this.rawValue=this.rawValue.replace(/n/g,"NA")
this.rawValue=this.rawValue.toUpperCase()
Oct 16 '14 #24

Rabbit
Expert Mod 10K+
P: 12,364
If it's testing fine then it's correct. But it can be cleaned up a little.
Expand|Select|Wrap|Line Numbers
  1. /^([1-9][0-9]?|100|n|r|i)$/
Oct 16 '14 #25

P: 14
Thanks so much for sticking with me on this. Your code got rid of the some of the extraneous stuff, and works properly! Thanks again for the help.
Oct 16 '14 #26

Rabbit
Expert Mod 10K+
P: 12,364
No problem, good luck with the rest of your project.
Oct 16 '14 #27

Post your reply

Sign in to post your reply or Sign up for a free account.