【发布时间】:2012-03-26 03:39:10
【问题描述】:
我有一个由用户输入的金额并想要验证它。我已经编写了一个 JSF 验证器,但在所有情况下都无法让它工作。这是我的场景:
我有不同区域的用户,因此我需要处理各种输入方法并希望允许以下操作
English
1234
1,234
1234.56
1,234.5
German & Spanish
1234
1.234
1234,56
1.234,5
French
1234
1 234
1234,56
1 234,5
我的问题是法语,因为使用此代码,选项 2 和 4 被视为无效,因为解析在空格处停止。
public void validate(final FacesContext pContext,
final UIComponent pComponent,
final Object pValue) {
boolean isValid = true;
final Locale locale = (Locale)pComponent.getAttributes().get(USERS_LOCALE);
final Currency currency = (Currency)pComponent.getAttributes().get(CURRENCY);
final NumberFormat formatter = NumberFormat.getNumberInstance(locale);
formatter.setGroupingUsed(true);
formatter.setMinimumFractionDigits(currency.getDefaultFractionDigits());
formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());
final ParsePosition pos = new ParsePosition(0);
final String stringValue = (String)pValue;
if (pos.getIndex() != stringValue.length() || pos.getErrorIndex() != -1) {
isValid = false;
}
...
我还想确保以下内容被视为无效但它们都成功解析(当然法语除外)
1,234,9.56(无效分组)
1,234.567(货币的小数位数太多)
任何帮助将不胜感激
伊恩
【问题讨论】:
标签: java localization number-formatting jsf-1.2