简体   繁体   English

如何仅使用PrimeFaces inputMask元素将输入限制为可变数量的数字

[英]How to restrict an input to a variable amount of numbers only with PrimeFaces inputMask element

I need to define something similar to this regex: 我需要定义类似于这个正则表达式的东西:

[0-9]{1, 5}

On a PrimeFaces <inputMask> element: 在PrimeFaces <inputMask>元素上:

<p:inputMask mask="regexGoesHere" value="#{someBean.val}"/>

I looked at the PrimeFaces showcase, but I couldn't figure out how to do it. 我看了PrimeFaces展示,但我无法弄清楚如何做到这一点。

So does anyone know how to do it in any way besides JavaScript ? 除了JavaScript之外,有没有人知道如何以任何方式做到这一点?

I'm not exactly looking for a solution with <inputMask> anything that would restrict me from typing letters in the input on the client side is OK. 我并不是在寻找一个带有<inputMask>的解决方案,这会限制我在客户端的输入中键入字母是可以的。

Thanks 谢谢

If you want or need to limit the length too, you could do something like this: 如果你想要或者也需要限制长度,你可以这样做:

<p:inputMask 
   mask="9?9999"
   maxlength="5"
   slotChar=" "
   value="#{someBean.val}" />

where the user can only enter 1 to 5 digits, or the following for four digits and so on 用户只能输入1到5位数字,或者输入以下四位数字,依此类推

<p:inputMask 
   mask="9?999"
   maxlength="4"
   slotChar=" "
   value="#{someBean.val}" />

Prior to PrimeFaces 5.1 : use placeHolder instead of slotChar ( Issue 7324 ). 在PrimeFaces 5.1之前 :使用placeHolder而不是slotChar问题7324 )。

以下Masked Input Plugin是Primefaces正在使用的原始jquery插件,你可以找到更多关于它的用法的信息,在这个PDF PrimeFaces中还有几个p:input-mask代码示例:更多输入元素请看第24页

KeyFilter from PrimeFaces Extensions looks exactly as something you need: http://fractalsoft.net/primeext-showcase-mojarra/views/keyFilter.jsf PrimeFaces Extensions的 KeyFilter看起来就像你需要的东西: http//fractalsoft.net/primeext-showcase-mojarra/views/keyFilter.jsf

According to documentation and example, it is driven by regexp, and functions exactly as it should: blocking the ability to type something not passing to the regexp. 根据文档和示例,它由regexp驱动,并且功能完全正确:阻止键入不传递给正则表达式的内容的能力。

just try this : 试试这个:

<p:inputMask maxlength="5">
        <pe:keyFilter regEx="/[\d]/" />
</p:inputMask>

maxlength : limit the number of caracters to 5 max regEx : only authorize decimal caracter on key press maxlength:将caracters的数量限制为5 max regEx:仅在按下时授权十进制字符

nb: 注意:

You could use a validator. 您可以使用验证器。 Or the validaterange and define a minimum and maximum. 或者验证范围并定义最小值和最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM