How to restrict Primefaces inputMask to numbers only?
Asked Answered
P

2

11

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>
Patiencepatient answered 12/12, 2014 at 4:59 Comment(0)
N
14

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;
  }
}
Nevels answered 12/12, 2014 at 16:44 Comment(3)
If you took a closer look, it's the same approach in both ways.Patiencepatient
Yes, I also mean both. Only difference is value attribute. Did you try to put it on InputMask?Nevels
I've just tried and post my solution and it works (accepts only numbers). I've posted it on my answer. Glad to be helpful.Nevels
G
0

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}"/>                         
Gath answered 4/6, 2021 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.