Mega Code Archive

 
Categories / Flex / Development
 

Triggering validation with Events

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="doValidate()">     <mx:Script>                      import mx.events.ValidationResultEvent;             import mx.validators.*;                          private var phoneV:PhoneNumberValidator;                          private function doValidate():void             {                 phoneV = new PhoneNumberValidator();                 phoneV.source = phoneTxt;                 phoneV.property = "text";                 phoneV.required = true;                 phoneV.allowedFormatChars = "+,-";                 phoneV.required = true;                 phoneV.allowedFormatChars = "+-";                 phoneV.invalidCharError = "Invalid char.";                 phoneV.wrongLengthError = "Invalid format";                 phoneV.requiredFieldError = "required";                      phoneV.trigger = phoneTxt;                 phoneV.triggerEvent = "change";             }                          private function isValid(evt:Event):void             {                 validationTxt.text  = evt.type as String;                 myLbl.visible = true;                 myLbl.text = "Validated!"                 myLbl.styleName = evt.type;             }                          private function notValid(evt:Event):void             {                                validationTxt.text  = evt.type as String;                 myLbl.visible = true;                 myLbl.text = "Correct the error in this field !"                 myLbl.styleName = "invalid";             }             private function handleResultEvent(evt:ValidationResultEvent):void {                                  if(evt.type==ValidationResultEvent.VALID)                   {                     validationTxt.text  = "The FormItem has been succesfully validated !";                                      }  else if(evt.type==ValidationResultEvent.INVALID)                  {                     validationTxt.text  = "The FormItem has NOT been validated !";                 }             }            </mx:Script>      <mx:Style>         .invalid {          color:#FF0000;         }         .valid {             color:#004000;         }      </mx:Style>     <mx:Form id="myForm">                 <mx:FormItem label="Insert Name" required="true">             <mx:HBox>                            <mx:TextInput id="nameTxt" valid="isValid(event)" invalid="notValid(event)" />                 <mx:Label id="myLbl" text="" visible="false" />             </mx:HBox>                   </mx:FormItem>         <mx:FormItem label="Insert Phone Number">             <mx:TextInput id="phoneTxt" />         </mx:FormItem>              </mx:Form>     <mx:TextArea x="10" y="172" width="305" height="87" id="validationTxt"/>      </mx:Application>