h:inputText值仅接受数字 [英] Accept only digits for h:inputText value

查看:193
本文介绍了h:inputText值仅接受数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JSF中确认h:inputText的值,该值只能接受数字.表示它可以是Integerfloat.

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.675a5678s1268712haf.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值仅接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆