PosNerd
PosNerd

Reputation: 33

How to validate numeric strings in inputText with validateRegex in a JSF-Page

I have a managed bean with this string property

private String itemNumber;

In the JSF I want to validate the input for length of 10 characters and allow only digits. The input will have preceding zeros. For example "0000123456"

<h:inputText value="#{testBean.itemNumber}">
  <f:validateRegex pattern="\d\d\d\d\d\d\d\d\d\d" />
</h:inputText>

On input of "asdf123456' I will get an beatiful validation message. But on input of "0000123456" I will get this exception

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

What is wrong? Any ideas?

Upvotes: 2

Views: 630

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

Declare the variable in the backing bean that is bound to your input text field as Integer/Long/BigDecimal and use

<f:validateLength maximum="10" minimum="10" />

instead of <f:validateRegex pattern="\d\d\d\d\d\d\d\d\d\d" />.

Upvotes: 3

Related Questions