Flex 一些数据的验证包括邮箱,字符创,日期,年龄等
使用方法:
?
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<mx:EmailValidator id="emailV" source="{inviteUserEmail}" trigger="{inviteButton}" triggerEvent="click" property="text" required="true"
? ??invalidCharError="非法字符"
????????invalidDomainError="非法域"
????????invalidIPDomainError="非法IP域"
????????missingAtSignError="缺少@符"
????????missingPeriodInDomainError="缺少域后缀"
????????missingUsernameError="缺少用户名"
valid="sendMessage(event)"/>
</fx:Declarations>
<s:Label x="37" y="30" text="{resourceManager.getString('language','datameeting.inviteuser.name')}" chromeColor="0x3483df" color="0x000000"/>
<s:Label x="37" y="72" text="{resourceManager.getString('language','datameeting.inviteuser.email')}" chromeColor="0x3483df" color="0x000000"/>
<s:Button x="58" y="119" label="{resourceManager.getString('language','datameeting.inviteuser.send')}" id="inviteButton" chromeColor="0x3483df" color="0xffffff"/>
<s:Button x="224" y="119" label="{resourceManager.getString('language','datameeting.inviteuser.quitsend')}" click="closeThis(event)" chromeColor="0x3483df" color="0xffffff"/>
<s:TextInput x="102" y="27" width="192" id="inviteUserName"/>
?
?
如果是多个组件都要一起验证,可以采用如下方法:
先创建一个validators集合
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<fx:Array id="validatorArray">
<mx:EmailValidator id="emailV" source="{inviteUserEmail}" property="text" required="true"/>
<mx:StringValidator source="{inviteUserName}" trigger="{inviteButton}" triggerEvent="click" property="text" required="true" minLength="NaN" maxLength="20"
requiredFieldError="{resourceManager.getString('language','required')}"
tooLongError="{resourceManager.getString('language','nametolong')}"/>
</fx:Array>
</fx:Declarations>
然后通过Validator的静态方法validateAll验证集合中的所有验证控件,返回一个ValidationResultEvent集合,如果集合为0,则通过验证!
var temp:Array = Validator.validateAll(validatorArray);
if(temp.length > 0){
return;
}
?
1.邮件验证
<mx:EmailValidator
????????id="emailV"
????????source="{txtEmail}"
????????property="text"
????????invalidCharError="非法字符"
????????invalidDomainError="非法域"
????????invalidIPDomainError="非法IP域"
????????missingAtSignError="缺少@符"
????????missingPeriodInDomainError="缺少域后缀"
????????missingUsernameError="缺少用户名"/>
2.字符串长度验证
<mx:StringValidator source="{fname}" property="text"
????????tooShortError="字符串太短了,请输入最少4个字符. "
????????tooLongError="字符串太长了,请输入最长20个字符. "
????????minLength="4" maxLength="20"
????????trigger="{myButton}" triggerEvent="click"
????????valid="Alert.show('字符串格式正确!');"/>??
3.日期验证
<mx:DateValidator source="{txtDate}" property="text"
????????required="true" requiredFieldError="请输入日期"
????????allowedFormatChars="-" inputFormat="YYYY-MM-DD"
????????trigger="{btnSubmit}" triggerEvent="click"
????????valid="mx.controls.Alert.show('验证成功');"
????????invalid="mx.controls.Alert.show('验证失败');"
????????wrongDayError="日输入错误"
????????wrongMonthError="月输入错误"
????????wrongYearError="年输入错误"
????????wrongLengthError="日期长度错误"
????????invalidCharError="日期分隔符错误"
????????formatError="inputFormat?配置错误"/>
4.英文字母验证
?<mx:RegExpValidator?id="regExpValidator"?
????????source="{txtInput}"?property="text"?
????????flags="g,i"?expression="^[a-z]+$"?
????????valid="handleResult(event)"?invalid="handleResult(event)"
????????trigger="{btnSubmit}"?triggerEvent="click"
????????noMatchError="请输入正确的英文字母"
????????required="false"/>
5.年龄验证
<mx:NumberValidator id="ageV" source="{ageTI}" property="text"
????domain="int"
????minValue="6"
????maxValue="100"
????lowerThanMinError="年龄过小!"
????exceedsMaxError="年龄过大!"
????integerError="年龄必须是整数!"
????invalidCharError='输入了非数字字符!'
????requiredFieldError="必须输入年龄!"
????trigger="{btn}" triggerEvent="click"/>
6.性别验证
<mx:Validator id="sexV" source="{sexRBG}" property="selectedValue"
????requiredFieldError="必须选择性别!"
????trigger="{btn}" triggerEvent="click"
????listener="{maleRB}"/>
???<mx:FormItem label="性别:" direction="horizontal" width="200">
????????<mx:RadioButtonGroup id="sexRBG">
????????????<mx:RadioButton id="maleRB" groupName="sexRBG" label="男" value="1"/>
????????????<mx:RadioButton id="femaleRB" groupName="sexRBG" label="女" value="0"/>
????????</mx:RadioButtonGroup>
???</mx:FormItem>
?