简体   繁体   中英

Action Script 3 NumericStepper validation for empty value

I have a simple NumericStepper which looks like this:

<mx:NumericStepper id="nsPort"
minimum="0"
maximum="65535"
stepSize="1"
value="{PORT_DEFAULT}"/>

I wrote a Validator for it (which doesn't work as I expect):

<mx:NumberValidator
required="true"
source="{nsPort}"
property="value"/>

However, every time I don't enter anything into NumericStepper it passes zero, not NULL or something else, which is...well...a valid value. I suspect that this is done on purpose. So, how can I make blank value at NumbericStepper to be invalid? Please note that I'm restricted to a ver.3 of Action Script.

Example code base from https://flexscript.wordpress.com/2008/09/22/flex-creating-custom-validators/ with a little modification:

package flexScript
{
     import mx.validators.ValidationResult;
     import mx.validators.Validator;

 //Class should extend mx.validators.Validator
 public class NumericStepperValidator extends Validator {

        public function NumericStepperValidator() {
            // Call base class constructor.
            super();
        }

        // Class should override the doValidation() method.
        //doValidation method should accept an Object type parameter
        override protected function doValidation(value:Object):Array {
            // create an array to return.
            var ValidatorResults:Array = new Array();
            // Call base class doValidation().
            ValidatorResults = super.doValidation(value);       
            // Return if there are errors.
            if (ValidatorResults.length > 0)
                return ValidatorResults;

            if (String(value).length == 0)
             return ValidatorResults;
            if ( value == 0)//as your required.
             return ValidatorResults;

               var RegPattern:RegExp = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/; //Change to your customize regExpression
            var a:Array = RegPattern.exec(String(value));
               if (a == null)
               {
                ValidatorResults.push(new ValidationResult(true, null, "NumbericStepper Error","You must enter an Number"));
                return ValidatorResults;
               }
             return ValidatorResults;
        }
    }
}

<flexScript:NumbericStepperValidator source="{{nsPort}}" property="number"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM