12

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Simeon
  • 7,582
  • 15
  • 64
  • 101

5 Answers5

29

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

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Nublodeveloper
  • 1,301
  • 13
  • 20
6

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

Daniel
  • 36,833
  • 10
  • 119
  • 200
3

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.

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
1

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:

Benjamin Fuentes
  • 674
  • 9
  • 22
  • 3
    Nothing in the question indicates that OP is using PrimeFaces Extensions. Therefore, this "try this" answer is confusing and worthless without a clear elaboration of the solution in words. – BalusC Apr 20 '15 at 10:08
  • u r quite rude with your comment @BalusC . i edited my answer so – Benjamin Fuentes May 06 '15 at 15:00
  • 4
    I just care about the quality of Stack Overflow, on contrary to all those people who post "try this" answers without teaching/explaining anything (being unable to teach/explain is a strong hint that you actually also don't exactly understand it by yourself). Teach how to fish instead of directly giving the fish. Otherwise the world ends up with only people who can't teach to fish anymore. If you find that rude, then that's just sad. – BalusC May 06 '15 at 15:02
  • good answer ... but not necessary to low score for workaround – Benjamin Fuentes May 07 '15 at 09:38
0

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

roel
  • 2,005
  • 3
  • 26
  • 41
  • validaterange is an attribute or an element or what ? If by validator you mean server side validation I already use the hibernate validator through the javax.validate annotations, but I want to restrict the user to pressing only number keys on the clinet. – Simeon Jan 13 '12 at 10:09