Validating a data model using the 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 phoneV:PhoneNumberValidator;
private var phoneVResult:ValidationResultEvent;
private var nameVResult:ValidationResultEvent;
private function doValidate():void
{
phoneV = new PhoneNumberValidator();
phoneV.source = myModel;
phoneV.property = "phone";
phoneV.listener=phoneTxt;
phoneV.required = true;
phoneV.allowedFormatChars = "+,-";
phoneV.invalidCharError = "Insert valid chars";
phoneV.wrongLengthError = "The number is too short or too long";
phoneV.requiredFieldError = "This input is required";
phoneVResult = phoneV.validate();
if(phoneVResult.type==ValidationResultEvent.VALID)
{
Alert.show("Validated!", "Validation Message", 4);
} else if(phoneVResult.type==ValidationResultEvent.INVALID)
{
Alert.show("NOT validated !", "Validation Message", 4);
}
}
</mx:Script>
<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