flex验证座机,手机的框架

 import mx.utils.StringUtil;
 import mx.validators.ValidationResult;
 import mx.validators.Validator;
 
 public class PhoneValidator extends Validator
 {
  public function PhoneValidator()
  {
   super();
  }
  
  private var _phoneInvalid:String="正确格式(010-865425433/010-865252352-1245/13933636356)";
  private var _phoneInvalidCode:String='10011';
  
  protected var phone1:RegExp = /^((0\d{2,3})-)(\d{6,9})$/ ;
  protected var phone2:RegExp = /^((0\d{2,3})-)(\d{6,9})(-(\d{1,5}))$/ ;
  protected var phone3:RegExp = /^\d{11}$/ ;
  
  private var _valtype:int = 0 ;
  
  public function get phoneInvalidError():String{
   return _phoneInvalid;
  }
  public function set phoneInvalidError(invalidString:String):void{
   this._phoneInvalid = invalidString;
  }
  
  //验证
  private function doPhoneValidation(validator:PhoneValidator, value:Object, baseField:String):Array{
   var rs:Array = [];
   var _ff:Boolean = false ;
   
   var _str:String = StringUtil.trim(String(value)) ;
   if(valtype==0 || valtype==1)
    _ff = phone3.test(_str) ;
   if( (valtype==0 || valtype==2))
   {
    if(_ff==false)
     _ff = phone2.test(_str) ;
    if(_ff==false)
     _ff = phone1.test(_str) ;
   }
   if(!_ff)
    rs.push(new ValidationResult(true,baseField,validator._phoneInvalidCode,validator._phoneInvalid));
   
   return rs;
  }
  
  override protected function doValidation(value:Object):Array{
   var rs:Array = super.doValidation(value);
   var val:String =  StringUtil.trim( value?String(value):'');
   if(rs.length >0 || ((val.length == 0) && !required) ){
    return rs;
   }
   
   return doPhoneValidation(this,value,null);
  }
  
  override protected function resourcesChanged():void
  {
   super.resourcesChanged();
   
   this.requiredFieldError="必填";
  }

  /**
   * 是否只是手机验证  0 手机加座机  1手机 2座机
   */
  public function get valtype():int
  {
   return _valtype;
  }

  /**
   * @private
   */
  public function set valtype(value:int):void
  {
   _valtype = value;
   if(value==0)
    _phoneInvalid = "正确格式(010-865425433/010-865252352-1245/13933636356)";
   else if(value==1)
    _phoneInvalid = "正确格式(13933636356)";
   else if(value==2)
    _phoneInvalid = "正确格式(010-865425433/010-865252352-1245)";
  }

 }
}

你可能感兴趣的:(Flex,验证框架)