Pages

Sunday, January 15, 2012

AS3: Form Validator

A form validator class
package engine
{ 
 
 /**
  * @file CFormValidator.as
  * @author Abhishek Kumar
  */
 
 import mx.controls.Alert;
 
 public class CFormValidator 
 {
  private var formItems:Array;
  
  public function CFormValidator()
  {
   formItems = [];
  }
  
  private function checkFormId(formId:String):void
  {
   if (formItems[formId] == null)
    formItems[formId] = [];
  }
  
  public function push(formId:String, elementId:Number):void
  {
   checkFormId(formId);
   formItems[formId][elementId] = 1;
  }
  
  public function pop(formId:String, elementId:Number):void
  {
   checkFormId(formId);
   formItems[formId][elementId] = 0;
  }
  
  public function check(formId:String, elementId:Number):Boolean
  {
   checkFormId(formId);
   for (var i:Number = 0, sum:Number = 0; i < formItems[formId].length; i++)
   {
    sum += formItems[formId][i];
   }
   return ((sum == elementId) ? true : false);
  }
  
  public function alert():void
  {   
   Alert.show('Please fill-up all the required fields!', 'Alert');
  }
 }
 
}

No comments:

Post a Comment