Babyangle86
Babyangle86

Reputation: 220

RegExpValidator never matches

I've got a class that's meant to validate input fields to make sure the value is always a decimal. I've tested the regex here: http://livedocs.adobe.com/flex/3/html/help.html?content=validators_7.html, and it looks like it does the right thing, but in my app, I can't seem to get it to match to a number format.

Class Definition:

public class DecimalValidator {
    //------------------------------- ATTRIBUTES
    public var isDecimalValidator:RegExpValidator;

    //------------------------------- CONSTRUCTORS
    public function DecimalValidator() { 
        isDecimalValidator                  = new RegExpValidator();
        isDecimalValidator.expression       = "^-?(\d+\.\d*|\.\d+)$";
        isDecimalValidator.flags            = "g";
        isDecimalValidator.required         = true;
        isDecimalValidator.property         = "text";
        isDecimalValidator.triggerEvent     = FocusEvent.FOCUS_OUT;
        isDecimalValidator.noMatchError     = "Float Expected";
    }

}

Setting the source here:

public function registerDecimalInputValidator(inputBox:TextInput, valArr:Array):void  {
        // Add Validators
        var dValidator:DecimalValidator = new DecimalValidator();
        dValidator.isDecimalValidator.source = inputBox;
        dValidator.isDecimalValidator.trigger = inputBox;

        inputBox.restrict = "[0-9].\\.\\-";
        inputBox.maxChars = 10;

        valArr.push(dValidator.isDecimalValidator);
    }

And Calling it here:

registerDecimalInputValidator(textInput, validatorArr);

Where textInput is an input box created earlier.

Clearly I'm missing something simple yet important, but I'm not entirely sure what! Any help would be much appreciated.

Upvotes: 0

Views: 1303

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

I don't know ActionScript, but as far as I know it's an ECMAScript language, so I expect you need to escape the backslashes if you use a string to define a regex:

isDecimalValidator.expression       = "^-?(\\d+\\.\\d*|\\.\\d+)$";

Upvotes: 1

JeffryHouser
JeffryHouser

Reputation: 39408

This strikes me as wrong; but I can't quite put my finger on it. For your DecimalValidator instead of composing a RegExpValidator; why not extend it?

public class DecimalValidator extend RegExpValidator{

//------------------------------- CONSTRUCTORS
public function DecimalValidator() {
   super()

    this.expression       = "^-?(\d+\.\d*|\.\d+)$";
    this.flags            = "g";
    this.required         = true;
    this.property         = "text";
    this.triggerEvent     = FocusEvent.FOCUS_OUT;
    this.noMatchError     = "Float Expected";
}
}

How when is the registerdecimalInputValidator called? I have a slight worry about the Validator instance is a local variable to a method instead of 'global' property to the function.

protected var dValidator:DecimalValidator = new DecimalValidator();

public function registerDecimalInputValidator(inputBox:TextInput):void  {
    dValidator.isDecimalValidator.source = inputBox;
    dValidator.isDecimalValidator.trigger = inputBox;

}

I'm not sure why you are setting restrictions on the TextInput in the registerDecimalInputValidator method; that should be done when you create the method (in createChildren() or possibly in response to public properties changing, in commitProperties . It is also not obvious to me what the validatorArr does. If you're expecting to access values inside the validatorArrray outside of the method; it would often be a common practice to return that value from the method. Without looking it up; I'm not sure if Arrays are passed by value or reference in Flex.

Upvotes: 1

Related Questions