问题描述
是否可以在JSF中确认h:inputText
的值,该值只能接受数字.表示它可以是Integer
或float
.
Is there a way to confirm the value of an h:inputText
in JSF, which should accepts only digits. Means it can be an Integer
or the float
.
如果我键入12s3a562.675
,a5678s12
,68712haf.563345
或任何其他此类值,则它应该显示错误.否则它将接受并继续.
If I type 12s3a562.675
, a5678s12
, 68712haf.563345
or any other such kind of values, then it should show an error. Otherwise it accepts and proceeds.
推荐答案
只需将输入值绑定到Double
或更佳的BigDecimal
属性而不是String
.
Just bind the input value to a Double
, or better, BigDecimal
property instead of String
.
private BigDecimal number; // Double can also, but beware floating-point-gui.de
<h:inputText value="#{bean.number}" />
对于这些类型,
JSF具有内置转换器自动加入.您可以自定义转换器消息,如下所示:
JSF has builtin converters for those types which will kick in automatically. You can customize the converter message as below:
<h:inputText value="#{bean.number}" converterMessage="Please enter digits only." />
这篇关于h:inputText值仅接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!