在 p:keyFilter 中使用正则表达式 [英] Using regex with p:keyFilter

查看:22
本文介绍了在 p:keyFilter 中使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 p:inputMask 和一个 p:keyFilter 来匹配 HH:MM 如下:

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>

但它不起作用,它接受从 00:0099:99 的所有值.

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

我该如何解决这个问题?

How can I solve this?

推荐答案

p:keyFilter vs f:validateRegexregEx vs inputRegEx

p:keyFilter 带有 regEx 属性用于过滤字符(在每次击键时),它不允许您验证表达式(在完整输入的价值).如果要验证输入是否与正则表达式匹配,请使用 inputRegEx 属性或 f:validateRegex.

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>

或:

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

请注意,p:keyFilter 需要 JavaScript 正则表达式,而 f:validateRegex 需要 Java 正则表达式.并且,p:keyFilter inputRegEx 在 key up 时被检查,而 f:validateRegex 在字段被处理时被执行.所以,在简历中:

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:

<头>
财产p:keyFilter inputRegEx="..."f:validateRegex pattern="..."
正则表达式类型JavaScriptJava
执行时间Key up JavaScript 事件被触发组件被处理

这适用于所有文本输入组件(如 p:inputText),不仅适用于您正在使用的 p:inputMask.

This applies to all text input components (like p:inputText), so not only to the p:inputMask you are using).

另见:

注意 p:keyFilter 从 6.0 开始可用.对于旧版本,您需要 PrimeFaces Extensions pe:keyFilter.请注意,6.0 之前的 PFE 版本与 PF 版本不一致.

Note that p:keyFilter is available since 6.0. For older versions you need PrimeFaces Extensions pe:keyFilter. Note that versions of PFE before 6.0 do not align with PF versions.

你可以看看pe:timePicker.此组件可能更适合您的需求.

You could have a look at pe:timePicker. This component might be more suitable for your needs.

这篇关于在 p:keyFilter 中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆