Announcement Announcement Module
Collapse
No announcement yet.
Validating a Float with regexp? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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...

    Comment


    • #3
      Secondly, have you considered using BigDecimal instead of float? 99.99 isn't really 99.99 when using a float.

      Comment


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

        Comment


        • #5
          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!

          Comment


          • #6
            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>

            Comment


            • #7
              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.

              Comment

              Working...
              X