Use Validator defined in MXML and call its validate method
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
import mx.validators.PhoneNumberValidator;
private var nameVResult:ValidationResultEvent;
private function doValidate():void
{
nameVResult = nameV.validate();
if(nameVResult.type==ValidationResultEvent.VALID) {
Alert.show("The FormItem has been succesfully validated !", "Validation Message", 4);
} else if(nameVResult.type==ValidationResultEvent.INVALID) {
Alert.show("The FormItem has NOT been validated !", "Validation Message", 4);
}
}
</mx:Script>
<mx:StringValidator id="nameV"
source="{myModel}"
property="surname"
required="true"
maxLength="10"
minLength="2"
tooLongError="This field contains 10 chars"
tooShortError="You have to insert 2 chars at least"
listener="{surnameTxt}"
/>
<mx:Model id="myModel">
<root>
<name>{nameTxt.text}</name>
<surname>{surnameTxt.text}</surname>
<phone>{phoneTxt.text}</phone>
</root>
</mx:Model>
<mx:Form id="myForm">
<mx:HBox>
<mx:TextInput id="nameTxt" />
<mx:Label id="myLbl" text="" visible="false" />
</mx:HBox>
<mx:FormItem label="Insert Surname">
<mx:TextInput id="surnameTxt" />
</mx:FormItem>
<mx:FormItem label="Insert Phone Number">
<mx:TextInput id="phoneTxt" />
</mx:FormItem>
</mx:Form>
<mx:Button label="Validate" click="doValidate();" x="284" y="172"/>
</mx:Application>
Related examples in the same category