Up to ValidatorExtensions


Question

 Does anyone have any recommendations for validating
 user-entered numbers with separators? (For example,
 the string "10,000.00".) Validator uses the Double
 constructor in its GenericTypeValidator.formatDouble
 method.

Answer

I wound up creating my own custom FieldChecks class with a validateDouble method that delegates to a new formatDouble method. Here's the code for formatDouble:

    public static Double formatDouble(String s) throws
             NumberFormatException {
        DecimalFormatSymbols syms = new DecimalFormatSymbols();
        StringBuffer buff = new StringBuffer();
        StringCharacterIterator iter = new StringCharacterIterator(s);
        for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            if(c != syms.getGroupingSeparator()) {
                buff.append(c);
            }
        }

        return GenericTypeValidator.formatDouble(buff.toString());
    }

Let me know if anyone has a better solution.

Matt Howitt


Up to ValidatorExtensions

  • No labels