Vivek Shankar
Vivek Shankar

Reputation: 21

h:inputText allow only decimal digits

I need to restrict the inputtext using onkeypress event to allow only numbers and decimal. i am able to restrict numbers but its not allowing dot value.

<h:inputText value="#{dimStackLine.max}"
  onkeypress="if( (event.which &lt; 48 || event.which &gt; 57) ) return false;">
  <p:ajax event="change" process="@this"></p:ajax>
  <f:convertNumber pattern="####0.00000" />
</h:inputText>

Upvotes: 0

Views: 874

Answers (1)

Selaron
Selaron

Reputation: 6184

Input of decimal dot . is prevented by your onkeypress because the event key code is 46 which is not within the allowed range of 48 <= code <= 57. You have to allow code 46 additionally:

<h:inputText value="#{dimStackLine.max}"
  onkeypress="if( (event.which &lt; 48 || event.which &gt; 57) &amp;&amp; event.which != 46 ) return false;">
  <p:ajax event="change" process="@this"></p:ajax>
  <f:convertNumber pattern="####0.00000" />
</h:inputText>

Upvotes: 2

Related Questions