Create an array with the validators
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initializeHandler(event)">
<mx:Script>
import mx.events.ValidationResultEvent;
import mx.controls.Alert;
import mx.validators.Validator;
private var _validators:Array;
private function initializeHandler(event:Event):void {
_validators = [usernameValidator, phoneValidator, emailValidator, zipCodeValidator];
}
private function runValidators(event:Event):void {
var results:Array = Validator.validateAll(_validators);
if(results.length > 0) {
for(var i:uint = 0; i < results.length; i++) {
Alert.show(results[i].target.source.id);
}
}
}
</mx:Script>
<mx:Form>
<mx:FormItem id="usernameItem" label="Name">
<mx:TextInput id="username"/>
</mx:FormItem>
<mx:FormItem label="Phone">
<mx:TextInput id="phone"/>
</mx:FormItem>
<mx:FormItem label="Email">
<mx:TextInput id="email"/>
</mx:FormItem>
<mx:FormItem label="Zip Code">
<mx:TextInput id="zipcode"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button id="button" label="Submit" click="runValidators(event)" />
</mx:FormItem>
</mx:Form>
<mx:Validator id="usernameValidator" source="{username}" property="text" />
<mx:PhoneNumberValidator id="phoneValidator" source="{phone}" property="text" />
<mx:EmailValidator id="emailValidator" source="{email}" property="text" />
<mx:ZipCodeValidator id="zipCodeValidator" source="{zipcode}" property="text" />
</mx:Application>
Related examples in the same category