简体   繁体   English

h:inputText只允许十进制数字

[英]h:inputText allow only decimal digits

I need to restrict the inputtext using onkeypress event to allow only numbers and decimal. 我需要使用onkeypress事件限制inputtext,以仅允许数字和十进制。 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 . 您的onkeypress可以防止此事件发生,因为事件键代码为46,但不在48 <= code <= 57的允许范围内。 You have to allow code 46 additionally: 您还必须允许代码46:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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