flex 自定义检验
?前些日子要用到一个到时间的检验,flex自带的只能检验yyyy-mm-dd的格式,呵呵,后来发现可以照的库里的自己写
public class TimeValidator extends Validator {
private static const errorFormat:String = "日期格式错误!,正确的格式是 HH24:mm:ss";
private static const errorHHValue:String = "小时的范围是 0-23!";
private static const errorMMValue:String = "分钟的范围是 0-59!";
private static const errorSSValue:String = "秒的范围是 0-59!";
public function TimeValidator() {
super();
}
override protected function doValidation(value:Object):Array {
var val:String = value ? String(value) : "";
// Return if there are errors
// or if the required property is set to false and length is 0.
return TimeValidator.validateTime(this, val, null);
}
public static function validateTime(validator:TimeValidator, value:Object, baseField:String = null):Array {
var results:Array = [];
// Resource-backed properties of the validator.
var regexp:RegExp = / \d{1,2}:\d{1,2}:\d{1,2}/x;
var val:String = value != null ? String(value) : "";
if (!regexp.test(val)) {
results.push(new ValidationResult(true, baseField, null, TimeValidator.errorFormat));
return results;
}
var t:Array = val.split(":");
var hh:int = int(t.shift());
var mm:int = int(t.shift());
var ss:int = int(t.shift());
if (hh < 0 || hh > 23) {
results.push(new ValidationResult(true, baseField, null, TimeValidator.errorHHValue));
return results;
}
if (mm < 0 || mm > 59) {
results.push(new ValidationResult(true, baseField, null, TimeValidator.errorMMValue));
return results;
}
if (ss < 0 || ss > 59) {
results.push(new ValidationResult(true, baseField, null, TimeValidator.errorSSValue));
return results;
}
return results;
}
}