Results 1 to 7 of 7

Thread: Validating a Float with regexp?

  1. #1

    Default Validating a Float with regexp?

    Hello

    I'm trying to validate a Float

    private final Constraint DRAUGHT_CONSTRAINT = all(new Constraint[] { new regexp( "/^\\d{0,2}(\\.\\d{0,2})?$/"), "xxx" });
    ...
    add("draught", DRAUGHT_CONSTRAINT);

    But regexp.test is expecting a String not a Float. So how do I do regexp's on Floats?

    I have tried this without any luck

    public class RegexpCastToStringConstraint extends RegexpConstraint {



    public RegexpCastToStringConstraint(String arg0) {

    rsten super(arg0);

    }



    public boolean test(Object value) {

    if (value == null)

    return true;

    return super.test(value.toString());

    }



    };

    BR
    Carsten

  2. #2
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    I find it quite strange that you'd use a regex for validating a float. Can you state you usecase or the validation that should happen? Using a regexp to solve a problem where it shouldn't results in 2 problems most of the time...
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  3. #3
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    Secondly, have you considered using BigDecimal instead of float? 99.99 isn't really 99.99 when using a float.
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  4. #4

    Default

    So how would it be done using BigDecimal instead of Float?

  5. #5
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    Well, on screen you'd use a NumberBinder for displaying the field, setting the amount of non-decimal and decimal numbers to 2 and negativeSign to false. This will allow the user to only enter numbers between 0.00 and 99.99. In that case, you won't even need validation. By the way, you can configure the NumberBinder to work with Float, too!
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  6. #6
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    The binder bean would look like this:

    Code:
    <bean id="positiveHundredBinder" class="org.springframework.richclient.form.binding.swing.NumberBinder">
        <property name="nrOfDecimals" value="2" />
        <property name="nrOfNonDecimals" value="2" />
        <property name="negativeSign" value="false" />
      </bean>
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  7. #7

    Default

    I ended up doing this:

    public class RegexpCastToStringConstraint extends RegexpConstraint {

    public RegexpCastToStringConstraint(String regexp, String type) {
    super(regexp);
    setType(type);
    }

    public boolean test(Object value) {
    if (value == null)
    return true;
    return super.test(value.toString());
    }

    };

    private final Constraint DRAUGHT_CONSTRAINT = all(new Constraint[] { new RegexpCastToStringConstraint(
    "\\d{0,2}(\\.\\d{0,2})?", "draughtConstraint") });

    add("draught", DRAUGHT_CONSTRAINT);

    As I'm trying to reuse validation from an existing app. that does all validation (more or less) through regexp's.

    Thanks for helping out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •