FLEX 中英文姓名校验器 VALIDATOR

为我那个无聊的缘分测试程序所写的中英文名字校验器,希望对大家能有作用

 

package com.love
{
 import mx.validators.Validator;
    import mx.validators.ValidationResult;

 public class NameValidator extends Validator
 {
  private var results:Array;
  
  private var isChinese:Boolean=false;
  private var isEnglish:Boolean=false;
  
  public function NameValidator()
  {
   super();
  }
  
  static public function isAsciiLetter( n:Number ):Boolean
  {
   if ( n >= 0x41 && n<= 0x5a ) return true;
   else if ( n >= 0x61 && n<= 0x7a ) return true;
   
   return false;
  }
  
  static public function isChineseCharacter( n:Number):Boolean
  {
   if ( n >= 0x3000 && n <= 0x9fff ) return true;   
   return false;   
  }
  
  static public function isUSLetter( n:Number):Boolean
  {
    if ( n >= 0x0530 && n <=0x058f ) return true;
    return false;
  }

   override protected function doValidation(value:Object):Array
   {

isChinese = false;
    isEnglish = false;
    
    var name:String = (String)(value);
    results = [];
   results = super.doValidation(value);
   
   if (results.length > 0)return results;
   
   
   if ( name.length < 2 || name.length >16 )
   {
    results.push(new ValidationResult(true, null, "Invalid",
                    "不正确的名字长度"));
    return results;
   }
      

    for ( var i:int=0;i<name.length;i++ )
    {
     var code:Number = name.charCodeAt(i);
     if ( isChineseCharacter (code) )
     {
      this.isChinese = true;
      //this.isEnglish = false;
     }
     else if ( isAsciiLetter(code) || isUSLetter(code) )
     {
      //this.isChinese = false;
      this.isEnglish = true;
     }
     else
     {
      this.isChinese = false;
      this.isEnglish = false;
     }
    }
    
    if ( (!isChinese && !isEnglish)  )
    {
     results.push(new ValidationResult(true, null, "Invalid",
                    "请使用纯中文或者纯英文的名字"));
     return results;
    }
    
    if ( isChinese && isEnglish )
    {
     results.push(new ValidationResult(true, null, "Invalid",
                    "请使用纯中文或者纯英文的名字"));
     return results;
    }
    
    return results;
    
   }

 }
 
 /*
              *  中国、日本和韩国的象形文字(总称为CJK)占用了从0x3000到0x9FFF的代码
               *  希腊字母表使用从0x0370到0x03FF的代码
               *  斯拉夫语使用从0x0400到0x04FF的代码
               *  美国使用从0x0530到0x058F的代码,补充ASCII码
               *  希伯来语使用从0x0590到0x05FF的代码
               */

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值