I'm trying to restrict input with Primefaces keyFilter
to match all numbers from 1 to 99 using regex. This is what I have at the moment:
<p:inputText id="inNum" maxlength="2">
<p:keyFilter regEx="/^[1-9][0-9]?$/" preventPaste="true" />
</p:inputText>
Input accepts the majority of desired number except those that have 0 as a second digit (10, 20, 30,..).
I can't figure it out why is that so. I've also tested that regular expression with online testers and it seems to be ok.
What am I missing here?
The regEx
attribute only checks a single keystoke with its regex, and since the ^[1-9][0-9]?$
pattern requires a non-zero digit as the first character, it only will allow a digit from 1 to 9. You need to check the whole input value with the inputRegEx
attribute.
You can use
inputRegEx='/^(?:[1-9][0-9]?)?$/'
Since you are using a regex for live validation input, you need to make sure that even an empty string passes the check, so all the subpatterns must be optional, and since you only allow non-zero digit at the start, the subpatterns must be sequentially optional.
Details
^
- start of string(?:[1-9][0-9]?)?
- an optional sequence of
[1-9]
- a non-zero digit[0-9]?
- an optional digit$
- end of string.