首页 > 其他分享 >Flex validator自定义校验 validator校验组件

Flex validator自定义校验 validator校验组件

时间:2022-11-19 21:10:30浏览次数:59  
标签:pwdError 自定义 results 校验 validator txt event

简单学习了一下 mx 的validator组件 ,以及自定义组件

<mx:StringValidator id="txt_name_string_validator" maxLength="20" minLength="6" property="text" source="{txt_name}" required="true" requiredFieldError="请输入用户名"
tooLongError="用户名太长,最长为20位."
tooShortError="用户名太短,最短6位."
trigger="{txt_name}" triggerEvent="click"
/>
<mx:StringValidator id="txt_pwd_string_validator" maxLength="20" minLength="6" property="text" source="{txt_pwd}" required="true" requiredFieldError="请输入密码"
tooLongError="用户名太长,最长为20位."
tooShortError="用户名太短,最短6位."
trigger="{txt_pwd}" triggerEvent="click"
/>
<validator:AccountValidator id="txt_name_account_validator" accountError="用户名只能输入字母,数字和下划线" property="text" source="{txt_name}"
trigger="{txt_name}" triggerEvent="click"
/>
<validator:PwdValidator id="txt_pwd_pwd_validator" pwdError="密码只能输入字母,数字" property="text" source="{txt_pwd}"
trigger="{txt_pwd}" triggerEvent="click"
/>
---------------------

自定义validator 需要继承Validator类

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

public class PwdValidator extends Validator
{
private var _pwdError:String ="只能为英文,数字";

public function PwdValidator()
{
super();
}


public function get pwdError():String
{
return _pwdError;
}

public function set pwdError(value:String):void
{
_pwdError = value;
}

override protected function doValidation(value:Object):Array
{
//定义返回值,并初始化
var results:Array;
results = [];

//执行父类的基本数据校验
results = super.doValidation(value);
if(results.length > 0){ //如果校验未通过,则终止继续执行。
return results;
}

//如果校验通过,则执行自定义的校验功能。
//利用正则表达式校验:只能为英文或数字

var regExp1:RegExp = new RegExp("[^A-Za-z0-9]");

//校验
if(regExp1.test(value.toString())){
//如果校验未通过,则生成一个校验结果
var vr:ValidationResult = new ValidationResult(true,null,"pwdError",pwdError);
//把校验结果加入到返回值数组中。
results.push(vr);
}

//返回校验结果
return results;
}
}

}
-----------------
当点击提交按钮时先进行组件验证方法:
private function validateBeforeLogin():Boolean{
var flag:Boolean = true;
var event:ValidationResultEvent = txt_name_string_validator.validate();
//执行校验
if(event.type == ValidationResultEvent.INVALID){
(event.target.source as UIComponent).setFocus();
flag = false;
}
event = txt_name_account_validator.validate();
//执行校验
if(event.type == ValidationResultEvent.INVALID){
(event.target.source as UIComponent).setFocus();
flag = false;
}
event = txt_pwd_string_validator.validate();
//执行校验
if(event.type == ValidationResultEvent.INVALID){
(event.target.source as UIComponent).setFocus();
flag = false;
}
event = txt_pwd_pwd_validator.validate();
//执行校验
if(event.type == ValidationResultEvent.INVALID){
(event.target.source as UIComponent).setFocus();
flag = false;
}
return flag;
}

标签:pwdError,自定义,results,校验,validator,txt,event
From: https://blog.51cto.com/u_15458282/5870594

相关文章