Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How validate number fields with validateRegex in a JSF-Page?

In a managed bean I have a property of the type int.

@ManagedBean
@SessionScoped
public class Nacharbeit implements Serializable {

private int number;

In the JSF page I try to validate this property for 6 digits numeric input only

               <h:inputText id="number"
                         label="Auftragsnummer"
                         value="#{myController.nacharbeit.number}"
                         required="true">
                <f:validateRegex pattern="(^[1-9]{6}$)" />
            </h:inputText>

On runtime I get an exception:

javax.servlet.ServletException: java.lang.Integer cannot be cast to java.lang.String

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

Is the regex wrong? Or are the ValidateRegex only for Strings?

like image 551
Twistleton Avatar asked Feb 29 '12 15:02

Twistleton


2 Answers

The <f:validateRegex> is intented to be used on String properties only. But you've there an int property for which JSF would already convert the submitted String value to Integer before validation. This explains the exception you're seeing.

But as you're already using an int property, you would already get a conversion error when you enter non-digits. The conversion error message is by the way configureable by converterMessage attribute. So you don't need to use regex at all.

As to the concrete functional requirement, you seem to want to validate the min/max length. For that you should be using <f:validateLength> instead. Use this in combination with the maxlength attribute so that the enduser won't be able to enter more than 6 characters anyway.

<h:inputText value="#{bean.number}" maxlength="6">
    <f:validateLength minimum="6" maximum="6" />
</h:inputText>

You can configure the validation error message by the validatorMessage by the way. So, all with all it could look like this:

<h:inputText value="#{bean.number}" maxlength="6"
    converterMessage="Please enter digits only."
    validatorMessage="Please enter 6 digits.">
    <f:validateLength minimum="6" maximum="6" />
</h:inputText>
like image 63
BalusC Avatar answered Oct 08 '22 00:10

BalusC


You can achieve this without regex also

To validate int values:

<h:form id="user-form">  
    <h:outputLabel for="name">Provide Amount to Withdraw  </h:outputLabel><br/>  
    <h:inputText id="age" value="#{user.amount}" validatorMessage="You can Withdraw only between $100 and $5000">  
    <f:validateLongRange minimum="100" maximum="5000" />  
    </h:inputText><br/>  
    <h:commandButton value="OK" action="response.xhtml"></h:commandButton>  
</h:form> 

To validate float values:

<h:form id="user-form">  
    <h:outputLabel for="amount">Enter Amount </h:outputLabel>  
    <h:inputText id="name-id" value="#{user.amount}" validatorMessage="Please enter amount between 1000.50 and 5000.99">  
    <f:validateDoubleRange minimum="1000.50" maximum="5000.99"/>  
    </h:inputText><br/><br/>  
    <h:commandButton value="Submit" action="response.xhtml"></h:commandButton>
</h:form>  
like image 22
Ajay Avatar answered Oct 08 '22 01:10

Ajay