Reputation: 129
I am working with Java Server Faces and Primefaces. One of my requirement is, do not allow only numbers in input text field(that means allow characters and special symbols). I can only allow the number by
<p:inputText value="#{doctorindBean.age}" id="age" tabindex="2" maxlength="30">
<pe:keyFilter mask="num"></pe:keyFilter>
</p:inputText>
Only characters by
<p:inputText value="#{doctorindBean.doclastname}" id="lastname" tabindex="2" maxlength="30">
<pe:keyFilter mask="alpha"></pe:keyFilter>
</p:inputText>
When I use 2nd one(i.e characters only), I can't use special characters.
But my scenario is to allow characters and special symbols. How can I do this?
Is there any other way to do this?
Upvotes: 1
Views: 15397
Reputation: 4853
Try with a nice trick by BalusC
bind it to an Integer property.
<h:inputText id="number" value="#{bean.number}" />
<h:message for="number" />
In Bean Class
private Integer number;
The form will not get submit, It'll show a conversion error when you submit non-digits. Codes Copied from here
If you Want Certain Prediction in Input Elements means go with <p:inputMask />
Updated
You can try the following scenario
public String setNumber(String number)
{
for(int i=0;i<mobile.length();i++)
{
if((int)number.charAt(i)>65 && (int)number.charAt(i)<90 || (int)number.charAt(i)>97 && (int)number.charAt(i)<122)
{
throw new NumberFormatException("Exception"); //Throwing manual exception
}
}
this.number=number;
}
It allows only Integer and Special Charactor....
Upvotes: 3