Validate form data with ActionScript : Form Validator « Components « Flex






Validate form data with ActionScript

Validate form data with ActionScript
           

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="550" height="300">
  <mx:Script>
    
      import mx.controls.Alert;
      import mx.events.ValidationResultEvent;
      private var validator:ValidationResultEvent;
            
      private function validate():void{
        var isValid:Boolean=true;
        if(!this.validateDataModel()){
          isValid=false;
        }
        Alert.show("Results = "+isValid,"Validation");
      }
      private function validateDataModel():Boolean{
        var isValid:Boolean = true;
        validator = fNameValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }
        validator = addressValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }
        validator = cityValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }
        validator = stateValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }
        validator = zipValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }
        validator = emailValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }
        validator = phoneValidator.validate();
        if(validator.type == ValidationResultEvent.INVALID){
          isValid = false;
        }  
        return isValid;    
      }
  
  </mx:Script>
  <mx:Model id="contactmodel">
    <contact>
      <fname>{fnametxt.text}</fname>
      <address>{addresstxt.text}</address>
      <city>{citytxt.text}</city>
      <state>{statetxt.text}</state>
      <zip>{ziptxt.text}</zip>
      <phone>{phonetxt.text}</phone>
      <email>{emailtxt.text}</email>
    </contact>
  </mx:Model>
  <mx:Validator id="fNameValidator" required="true" source="{contactmodel}" property="fname" listener="{fnametxt}"/>
  <mx:Validator id="addressValidator" required="true" source="{contactmodel}" property="address" listener="{addresstxt}"/>
  <mx:Validator id="cityValidator" required="true" source="{contactmodel}" property="city" listener="{citytxt}"/>
  <mx:Validator id="stateValidator" required="true" source="{contactmodel}" property="state" listener="{statetxt}"/>  
  <mx:ZipCodeValidator id="zipValidator" required="true" source="{contactmodel}" property="zip" listener="{ziptxt}"/>
  <mx:EmailValidator id="emailValidator" source="{contactmodel}" property="email" listener="{emailtxt}"/>
  <mx:PhoneNumberValidator id="phoneValidator" source="{contactmodel}" property="phone" listener="{phonetxt}"/>
  <mx:Panel title="Contact Info" width="500" height="250">
  <mx:Form>
    <mx:FormItem label="Name" direction="horizontal">
      <mx:TextInput id="fnametxt" />
    </mx:FormItem>
    <mx:FormItem label="Address">
      <mx:TextInput id="addresstxt"/>
    </mx:FormItem>
    <mx:FormItem label="City State, Zip" direction="horizontal">
      <mx:TextInput id="citytxt"/>
      <mx:TextInput id="statetxt" width="20"/>
      <mx:TextInput id="ziptxt" width="50"/>
    </mx:FormItem>
    <mx:FormItem label="Phone">
      <mx:TextInput id="phonetxt"/>
    </mx:FormItem>
    <mx:FormItem label="Email">
      <mx:TextInput id="emailtxt"/>
    </mx:FormItem>
    <mx:Button id="validatebtn" label="Validate Data Model" click="validate()"/>
  </mx:Form>
  </mx:Panel>
</mx:Application>

   
    
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Using Flex validators with form modelsUsing Flex validators with form models
2.Set up validator for Form input fieldsSet up validator for Form input fields
3.Use Validator to validate data from a formUse Validator to validate data from a form
4.Validator, Model and Form controlsValidator, Model and Form controls
5.Using Validators.validateAll() method to validate a formUsing Validators.validateAll() method to validate a form
6.Validating form control contents data on user entryValidating form control contents data on user entry
7.Validating user input for form dataValidating user input for form data
8.Change form input box label when data is not validChange form input box label when data is not valid
9.Use ActionScript to validate form dataUse ActionScript to validate form data
10.Validate a date entered into a formValidate a date entered into a form
11.Validate an e-mail address to ensure that it is formatted correctlyValidate an e-mail address to ensure that it is formatted correctly