简体   繁体   中英

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM