Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jsf - Using regex with p:keyFilter

I have an p:inputMask with a p:keyFilter to match HH:MM as following:

<p:inputMask mask="99:99" ...>
     <p:keyFilter regEx="([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]"/>
</p:inputMask>

But it doesn't work and it accepts all values from 00:00 to 99:99.

How can I solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

p:keyFilter versus f:validateRegexregEx versus inputRegEx

p:keyFilter with the regEx attribute is used to filter characters (on each key stroke), it does not allow you to validate an expression (on the complete inputted value). If you want to validate if your input matches a regular expression, use the inputRegEx attribute or f:validateRegex.

So, in your case you could use:

<p:inputXxx ...>
    <f:validateRegex pattern="([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]"/>
</p:inputXxx>

or:

<p:inputXxx ...>
    <p:keyFilter inputRegEx="/([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]/"/>
</p:inputXxx>

Please note that p:keyFilter requires JavaScript regular expressions, while, while f:validateRegex requires a Java regular expression. And, p:keyFilter inputRegEx is checked on key up, while f:validateRegex is executed when the field is processed. So, in resume:

Property p:keyFilter inputRegEx="..." f:validateRegex pattern="..."
Regular expression type JavaScript Java
Executed when Key up JavaScript event is triggered Component is processed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...