0

Within Livecycle, I am validating that the number entered is a 0 through 10 and allows quarter hours. With the help of this post, I've written the following.

if (!xfa.event.newText.match(/^(([10]))$|^((([0-9]))$|^((([0-9]))\.?((25)|(50)|(5)|(75)|(0)|(00))))$/))
    {
        xfa.event.change = "";
    };

The problem is periods are not being accepted. I have tried wrapping the \. in parenthesis but that did not work either. The field is a text field with no special formatting and the code in the change event.

Community
  • 1
  • 1
turkaffe
  • 100
  • 2
  • 12

1 Answers1

1

Yikes, that's a convoluted regex. This can be simplified a lot:

/^(?:10|[0-9](?:\.(?:[27]?5)?0*)?)$/

Explanation:

^             # Start of string
(?:           # Start of group:
 10           # Either match 10
|             # or
 [0-9]        # Match 0-9
 (?:          # optionally followed by this group:
  \.          # a dot
  (?:[27]?5)? # either 25, 75 or 5 (also optional)
  0*          # followed by optional zeroes
 )?           # As said before, make the group optional
)             # End of outer group
$             # End of string

Test it live on regex101.com.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thank you for including the explanation! It is almost working, it won't accept the `2` or `7`. – turkaffe Jan 28 '15 at 16:14
  • @silviak: Not sure what you mean. It should accept `25` or `75`, and it shouldn't accept `2` or `7`, right? I've added a live preview at regex101. – Tim Pietzcker Jan 28 '15 at 16:17
  • Testing `.25` or `.75`, when I type in `.25` it will not accept the `2` and skips over to the `5`. I'm thinking maybe because it is on the change event and it looking for the entire string? – turkaffe Jan 28 '15 at 16:50
  • Oh, so you're checking the input as you type? Not a good idea - if `0.7` is invalid and `0.75` is valid, how should the computer know that you intend to type another `5` after the `0.7`? Validation should only be done after you've submitted the entry. – Tim Pietzcker Jan 28 '15 at 17:13