为我那个无聊的缘分测试程序所写的中英文名字校验器,希望对大家能有作用
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的代码
*/
}