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>
分享到:
相关推荐
CheckForm.js 常用表单javascript验证
Dreamweaver CHECKFORM.MXP 插件
checkform.js对一般的表单验证进行了封装,方便实用
js 验证页面 checkform 验证页面的js代码
适合新手的简单的表单验证,纯js编写,适合js初学者练习
本程序是用于方便无人值守,自动重启电脑,并可以设定多个时间重启电脑,并且写入注册表,开机自启动
4:js只能输入数字,判断数字、验证数字、检测数字、判断是否为数字、只能输入数字 5:只能输入英文字符和数字 6: js email验证 、js 判断email 、信箱/邮箱格式验证 7:js字符过滤,屏蔽关键字 8:js密码验证、判断密码 ...
js验证表单大全js验证radio是否选择 <script language="javascript">function checkform(obj){for(i=0;i<obj.oo.length;i++) if(obj.oo[i].checked==true) return true; alert("请选择")return false; }</script>...
js验证表单大全 例如: <!-- function CheckForm() { if (document.form.count.value > 100 || document.form.count.value ) { alert("输入数值不能小于零大于100!"); document.form.count.focus(); return ...
Jquery checkform 插件基于输入的 name 属性的常规命名规则。 如果您遵守这些规则,则只需添加以下 javascript 行 $("#myForm").checkform() 对于更复杂的情况或自定义默认模式,请查看文档页面 文件说明 css/ ...
input输入密码加密 ... <form id=login-form method=post onsubmit=return checkForm()> ...input type=password id=input-password>...js代码 function checkForm() { var input_pwd = document.getEl
<script type="text/javascript"> window.onload = loadForm; function loadForm(){ for(i = 0;i<document.forms.length;i++){ if(document.forms[i].getAttribute("name")=="oform"){ checkForm(document....
input type="password" id="input1"> <input type="password" id="input2"> <input type="button" value="test" </FORM> [removed] function check() { with(document.all){ if(input1.value!=...
jquery-checkform
checkform-for.netcheckform-for.net
很小巧的一个JS验证框架。
input type=password id=input-password> <input type=hidden id=md5-password name=password> <button type=submit>Submit</button> </form> js代码 function checkForm() { v
本文实例为大家分享了js实现用户注册功能的具体代码,供大家参考,具体内容如下 1.HTML代码结构 <BODY> <FORM action="success.html" method="post" name="myform" onSubmit="return checkForm()"> border=...
日历控件 后台路径问题: 所有处理后台管理员工作的Servlet的url-pattern /manage/........ 首页(新闻,商品 ,留言,商品大分类,商品小...表单验证 checkForm 用户注册 用户名唯一 AJAX 登录 (验证码非空验证/及时验证)
表单验证一直很繁琐,特别是大点的表单,如果每个input都去单独写验证简直要写死人,最近写了一小段js统一的验证表单内容是否正确。 使用这段代码就不再需要对每个input写格式判断,只需要将正确格式的正则表达式写...