我有一个用户输入的金额,想要验证它.我已经编写了一个JSF验证器,但是无法在所有情况下都能使用它.这是我的场景:
我有不同的Locales用户,因此我需要处理各种输入方法,并希望允许以下
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
Run Code Online (Sandbox Code Playgroud)
我的问题是法语,因为解析在空格处停止时,使用此代码将选项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;
} …Run Code Online (Sandbox Code Playgroud)