Flex多功能输入验证控件ValidInput(仿CheckForm.js),抛弃Flex Validator另辟蹊径.
package com.ux.utils
{
import com.ux.components.FileInput
import com.ux.components.ValidInput;
import com.ux.autocomplete.AutoComplete;
import mx.core.Container;
public class ValidateUtil
{
public function ValidateUtil()
{
}
public static function isValid(container:Container):Boolean
{
var ISVALID:Boolean=true;
ISVALID=ISVALID && recursive(container.getChildren());
return ISVALID;
}
/**
* 遍历验证控件
* @return Boolean
* */
protected static function recursive(targets:Array):Boolean
{
var VALID:Boolean=true;
if (0 < targets.length)
{
for each (var o in targets)
{
if (o is Container && !(o is ValidInput) && !(o is AutoComplete) && !(o is FileInput))
{
VALID=VALID && recursive(o.getChildren());
}
else
{
if (o is ValidInput)
{
if (!(o as ValidInput).doValidate())
{
VALID=false;
ValidInput(o).setFocus();
break;
}
}
if (o is AutoComplete)
{
if (!(o as AutoComplete).doValidate())
{
VALID=false;
break;
}
}
if(o is FileInput){
if(!(o as FileInput).doValidate()){
VALID = false;
break;
}
}
}
}
}
return VALID;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Metadata>
[IconFile("com/ux/components/assets/icon.png")]
</mx:Metadata>
<mx:Style>
.errorTip{
fontSize:12;
}
</mx:Style>
<mx:Script>
<![CDATA[
[Bindable]
private var _displayAsPassword:Boolean=false;
[Bindable]
private var _editable:Boolean=true;
private var _format:String=null;
[Inspectable(type="Boolean",defaultValue="false",enumeration="true,false")]
public function set displayAsPassword(displayAsPassword:Boolean):void
{
_displayAsPassword=displayAsPassword;
}
[Inspectable(type="Boolean",defaultValue="true",enumeration="true,false")]
public function set editable(editable:Boolean):void
{
_editable=editable;
}
public function set format(format:String):void
{
_format=format;
}
public function doValidate():Boolean
{
if (null != _format)
{
var value:String=Text();
var postion:int=_format.lastIndexOf("~");
var errorInfo:String=_format.substring(0, postion);
var format:String=_format.substring(postion + 1, _format.length);
//判断不能为空
var notNull:Boolean=false;
if (format.charAt(format.length - 1) == "!")
{
notNull=true;
format=format.substring(0, format.length - 1);
}
if (notNull)
{
if ("" == value)
{
validInput.errorString=errorInfo + ",内容不能为空!";
return false;
}
else
{
validInput.errorString="";
}
}
//判断不能为空
//内容的长度判断
var colonP:int=format.indexOf(":");
if (colonP > 0)
{
if (format.charAt(colonP - 1) == 'f')
{
var lengthLimit:String=format.substring(0, colonP - 1);
var lNaN:int=parseInt(lengthLimit);
if (!isNaN(lNaN))
{
var len:Number=getRealLength(value);
if (len != lNaN)
{
validInput.errorString=errorInfo + "," + "长度为(" + len + ")位,必须为(" + lengthLimit + ")位!";
return false;
}
else
{
validInput.errorString="";
}
}
}
else
{
var limitStr:String=format.substring(0, colonP);
var limitNaN:int=parseInt(limitStr);
if (!isNaN(limitNaN))
{
var lenth:Number=getRealLength(value);
if (lenth > limitNaN)
{
validInput.errorString=errorInfo + "," + "长度(" + lenth + ")超过限制(" + limitNaN + ")位!";
return false;
}
else
{
validInput.errorString="";
}
}
}
}
format=format.substring(colonP+1, format.length);
//判断类型
switch (format)
{
case "email": //判断Email
if (null == value.match(/\w+@.+\..+/))
{
validInput.errorString=errorInfo + "\n" + "格式不正确:\"" + value + "\"不是一个Email地址";
return false;
}
else
{
validInput.errorString="";
}
break;
case "int": //判断Int
var intVal:*=parseInt(value);
if (isNaN(intVal) || intVal != value)
{
validInput.errorString=errorInfo + "\n" + "格式不正确:" + value + "不是一个整数。";
return false;
}
else
{
validInput.errorString="";
}
break;
case "float": //判断float
if(null == value.match(/^(-?\d+)(\.\d+)?$/))
{
validInput.errorString=errorInfo + "\n" + "格式不正确:" + value + "不是一个浮点数。";
return false;
}
else
{
validInput.errorString="";
}
break;
case "card":
if (null == value.match(/^\d{15}|\d{18}$/))
{
validInput.errorString=errorInfo + "\n" + "格式不正确:" + value + "不是一个有效身份证号。";
return false;
}
else
{
validInput.errorString="";
}
break;
case "phone":
if(null == value.match(/^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/)){
validInput.errorString=errorInfo + "\n" + "格式不正确!";
return false;
}else{
validInput.errorString="";
}
break;
case "login":
if(null == value.match(/^[a-zA-Z]\w{5,17}$/)){
validInput.errorString=errorInfo + "\n" + "格式不正确!";
return false;
}else{
validInput.errorString="";
}
break;
case "url":
if(null == value.match(/^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$|^[a-zA-z]+:\/\/(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$/)){
validInput.errorString=errorInfo + "\n" + "格式不正确!";
return false;
}else{
validInput.errorString="";
}
break;
default:
trace("FORMAT_STR: " + format + " , FORMAT String undefined!");
}
}
return true;
}
/**
*获取字符串真实长度
* @return number
* */
private function getRealLength(val:String):Number
{
var len:Number=0;
for (var i:int=0; i < val.length; i++)
{
if (val.charCodeAt(i) > 255)
len+=2;
else
len++;
}
return len;
}
/**
* 去除字符串两端的空格
* @return String
* */
private function trim(s:String):String{
s=s.replace(/^ */,"");
s=s.replace(/ *$/,"");
return s;
}
/**
* 获取控件值
* @return text:String
* */
public function Text():String
{
return validInput.text;
}
]]>
</mx:Script>
<mx:TextInput width="100%"
height="100%"
id="validInput"
displayAsPassword="{_displayAsPassword}"
editable="{_editable}"
focusOut="doValidate();"/>
</mx:HBox>
本文介绍了一种使用Flex实现的多功能输入验证控件ValidInput,旨在简化验证逻辑并替代传统FlexValidator。通过遍历容器内的验证控件,如文件输入、自动补全等,并对每个控件进行验证,确保输入数据的有效性和格式符合要求。
757

被折叠的 条评论
为什么被折叠?



