简体   繁体   中英

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:

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

I looked at the PrimeFaces showcase, but I couldn't figure out how to do it.

So does anyone know how to do it in any way besides 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.

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

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

Prior to PrimeFaces 5.1 : use placeHolder instead of slotChar ( Issue 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

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.

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

nb:

You could use a validator. Or the validaterange and define a minimum and maximum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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