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

后端 未结 5 2372
Happy的楠姐
Happy的楠姐 2021-02-20 05:35

I need to define something similar to this regex:

[0-9]{1, 5}

On a PrimeFaces element:



        
相关标签:
5条回答
  • 2021-02-20 05:50

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

    0 讨论(0)
  • 2021-02-20 05:56

    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.

    0 讨论(0)
  • 2021-02-20 05:59

    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).

    0 讨论(0)
  • 2021-02-20 05:59

    The following Masked Input Plugin is the original jquery plugin that is being used by Primefaces , you can find much more information about its usage, also there are several p:input-mask code example in this PDF PrimeFaces: More Input Elements look at page 24

    0 讨论(0)
  • 2021-02-20 06:03

    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:

    0 讨论(0)
提交回复
热议问题