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

查看:27
本文介绍了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 属性而不是 字符串.

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天全站免登陆