1

I need an input component to accept only numbers as in [0-9]. I have achieved this by server side conversion/validation:

<h:inputText ... converter="javax.faces.Integer" />

However, it still allows other characters while entering in the UI. How can I prevent that during the keypress event?

<h:inputText ... converter="javax.faces.Integer" onkeypress="...help?" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Satya Bali
  • 11
  • 2
  • This is not exactly a JSF problem, but just JavaScript. In that case, this question is a dupe: [Best way to restrict a text field to numbers only?](http://stackoverflow.com/questions/3764821/best-way-to-restrict-a-text-field-to-numbers-only) – BalusC Apr 05 '13 at 13:10

2 Answers2

0

Try this

<h:inputText id="presentReading" styleClass="labelfont" label="Present Reading"    converter="javax.faces.Integer"value="#{FuelFilling1.presentReading}" maxlength="10">
   <f:validateLongRange minimum="1" maximum="10"/>
</h:inputText>

Alternatively, you can use JQuery or Javascript to validate on the client-side on keypress. Are you okay to use Jquery?

IndoKnight
  • 1,846
  • 1
  • 21
  • 29
0

I would recommend sticking with jsf validation.

<f:convertNumber integerOnly="true" type="number" />

You can use ajax in order to validate in real time if you don't want to have to submit the form to the get response. Doing keypress validation with javascript/jQuery can be done, but is a pain unless you have a really good reason to do it that way.

Mike
  • 358
  • 1
  • 2
  • 12