How to restrict Primefaces inputMask to numbers only?

35k views Asked by At

I want to restrict p:inputMask to numbers only, and I tried the proposed solutions from:

But none of them worked. I tried these two ways:

<p:inputMask id="userNo"  maxlength="2" mask="9?9999" />

and

<p:inputMask id="userNo"  maxlength="2" >
    <pe:keyFilter regEx="/[0-9_]/i"/>
</p:inputMask>
2

There are 2 answers

3
giaffa86 On BEST ANSWER

I think that approach is right but I noticed that you don't put value attribute on inputMask. Do you try to put it on?

Edit: These solutions works:

inputMaskTest.xhtml:

<h3>Input Mask:</h3>
<h:form id="form">
    <p:outputLabel value="Input Mask only number " for="userNo1" />
    <p:inputMask id="userNo1"  maxlength="2" mask="9?9999" value="#{inputMaskView.number}"/>

    <p:outputLabel value="Input Mask only Number Primeface Ext " for="userNo2" />
    <p:inputMask id="userNo2" maxlength="2" value="#{inputMaskView.number}">
        <pe:keyFilter regEx="/[0-9_]/i" />
    </p:inputMask>
</h:form>

InputMaskView.java:

@ManagedBean(name = "inputMaskView")
@ViewScoped 
public class InputMaskView {
  private String number;

  public String getNumber() {
    return number;
  }
  public void setNumber(String number) {
    this.number = number;
  }
}
0
Blas Albir On

Whole number only, no decimal, no negative, no thousand separator.

It has several applications, for example code reception.

It is like:

<p:inputNumber minValue="1" decimalPlaces="0" thousandSeparator="" value="#{myclass.myVariable}"/>