Raphael Roth
Raphael Roth

Reputation: 27373

primefaces inputText enter string, but only allow digits

I have a primefaces p:inputText where I want the user to enter an integer, but the type of the data must remain a string as I use server-side bean validation with @javax.validation.constraints.Size (Thats why integer converters do not work, I get an ClassCastExcpetion from the Hibernate validator). I do that because the seam bean is also used to store string on other occasions.

Ideally, I want that the user can only enter digits in the field, therefore I played with "onkeypressed" and different regexes/keycodes, but I could not found a solution that worked on Chrome, Firefox and IE.

Any ideas?

EDIT:

I experimented with f:validateRegex, but strangely the validation does only work if I omit the type="number" in p:inputText. Also the posted solution of Paul Wasilewski does not work in the case of input="number" (at least on firefox)

Upvotes: 2

Views: 3356

Answers (1)

Paul Wasilewski
Paul Wasilewski

Reputation: 10372

If you have not tried it already. A solution would be

<p:inputText ... onkeydown="return event.keyCode &gt;= 48 &amp;&amp; event.keyCode &lt;= 57 || event.keyCode == 8" ... />

Upvotes: 2

Related Questions