在Flex中用Validator检测数字、字符串、Email、电话号码等 .

在Flex中用Validator检测数字、字符串、Email、电话号码等
在上例“ 用Validator检测必填项”中,我记录了Validator最简单的用法。但mx.validators包中的类并非只有Validator一个,他们可以实现信用卡号码格式检测(mx.validators.CreditCardValidator)、货币格式检测(mx.validators.CurrencyValidator)、E-mail格式检测(mx.validators.EmailValidator)等等功能,所有的检测器列表可以看 这里。这些类都是Validator的子类。

先看效果。下面的范例演示了StringValidator、NumberValidator和EmailValidator的用法。由于这三个类都继承自Validator,因此都拥有requiredFieldError属性,用于自定义没有值的时候的错误信息。但是这三个类拥有更多的错误信息。要检测的值越复杂,需要定义的错误信息就越多,例如EmailValidator,本例中共定义了9个错误信息。

如果不定义错误信息,Flex会显示默认的英文错误信息,这显然也不是我们所需要的。如果想偷懒的话,可以定义几个最可能出现的错误,例如本例的NumberValidator,就没有定义所有的错误信息。

顺便还要说一句的是,NumberValidator可以指定要检测的数字是整数还是实数,这需要用domain属性来指定。同时,它还可以指定千分位分隔符。

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" fontSize="12" width="300" height="200">

 <mx:Style>
 <![CDATA[
 .errorTip
 {
 fontSize: 12;
 }
 ]]>
 </mx:Style>

 <mx:StringValidator id="nameV" source="{nameTI}" property="text"
 minLength="2"
 maxLength="5"
 requiredFieldError="必须输入姓名!"
 tooShortError="姓名过短!"
 tooLongError="姓名过长!"
 trigger="{btn}" triggerEvent="click"/>

 <mx:NumberValidator id="ageV" source="{ageTI}" property="text"
 domain="int"
 minValue="6"
 maxValue="100"
 lowerThanMinError="年龄过小!"
 exceedsMaxError="年龄过大!"
 integerError="年龄必须是整数!"
 invalidCharError='输入了非数字字符!'
 requiredFieldError="必须输入年龄!"
 trigger="{btn}" triggerEvent="click"/>

 <mx:Validator id="sexV" source="{sexRBG}" property="selectedValue"
 requiredFieldError="必须选择性别!"
 trigger="{btn}" triggerEvent="click"
 listener="{maleRB}"/>

 <mx:EmailValidator id="emailV" source="{emailTI}" property="text"
 requiredFieldError="必须输入E-mail"
 invalidCharError="E-mail地址中有错误字符。"
 invalidDomainError="E-mail地址中的域名不符合规范。"
 invalidIPDomainError="E-mail地址中的IP格式域名不符合规范。"
 invalidPeriodsInDomainError="域名中的“.”错误。"
 missingAtSignError="E-mail地址缺少“@”符号。"
 missingPeriodInDomainError="域名中缺少“.”"
 missingUsernameError="E-mail地址缺少用户名。"
 tooManyAtSignsError="E-mail地址中的“@”符号太多。"
 trigger="{btn}" triggerEvent="click"/>
 
 <mx:FormItem label="姓名:" width="200">
 <mx:TextInput id="nameTI"/>
 </mx:FormItem>

 <mx:FormItem label="年龄:" width="200">
 <mx:TextInput id="ageTI"/>
 </mx:FormItem>

 <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:FormItem>

 <mx:FormItem label="E-mail:" width="200">
 <mx:TextInput id="emailTI"/>
 </mx:FormItem>

 <mx:Button id="btn" label="提交" />
<script lang="tsx" setup>   import { stringToBase64 } from './utils/index';   import { ref, watchEffect } from 'vue';   import { renderToString } from '@vue/server-renderer';   interface IProps {     defaultDurTime?: number; //进度条完结时间     progress?: number; //当前进度0-100     color?: object | string; //进度条颜色,传入对象格式可以定义渐变色     width?: number; //矩形宽度     height?: number; //矩形高度     radian?: number; //矩形圆角弧度     layerColor?: string; //轨道颜色     fill?: string; //填充颜色     strokeWidth?: number; //线条宽度     strokeLinecap?: string; //进度条端点(butt,round,square)   }   const props = withDefaults(defineProps<IProps>(), {     defaultDurTime: 1000, //进度条完结时间     progress: 50,     color: () => {       return { '0': '#80EDFF', '100': '#4A7DFF' };     },     width: 140,     height: 120,     radian: 20,     layerColor: '#e0e0e0',     fill: 'none',     strokeWidth: 10,     strokeLinecap: 'round',   });   const progressOffset = ref(0);   const allLen = ref(0);   const strokeBgColor = ref<string | object>('#666666');   let dataURL = ref('');   async function handleSave() {     const html = await renderToString(renderTitle());     dataURL.value = stringToBase64(html);     console.log(dataURL);   }   const renderTitle = () =>     return (       <svg         width={props.width + props.strokeWidth}         height={props.height + props.strokeWidth}         xmlns="http://www.w3.org/2000/svg"       >         {Object.prototype.toString.call(props.color) === '[object Object]' && (           <defs>             <linearGradient id="progressGradient" x1="0%" y1="0%" x2="100%" y2="0%">               {Object.keys(props.color).map((key) => (                 <stop offset={`${key}%`} stop-color={props.color[key]} key={key}></stop>               ))}             </linearGradient>           </defs>         )}         <rect           x={0.5 * props.strokeWidth}           y={0.5 * props.strokeWidth}           rx={props.radian}           ry={props.radian}           width={props.width}           height={props.height}           style={{ strokeWidth: props.strokeWidth, fill: props.fill, stroke: props.layerColor }}         />         {props.progress !== 0 && (           <rect             x={0.5 * props.strokeWidth}             y={0.5 * props.strokeWidth}             rx={props.radian}             ry={props.radian}             width={props.width}             height={props.height}             style={{               strokeDashoffset: progressOffset.value,               strokeDasharray: allLen.value,               strokeWidth: props.strokeWidth,               fill: props.fill,               stroke: strokeBgColor.value,               strokeLinecap: props.strokeLinecap,             }}           >             <animate               attributeName="stroke-dashoffset"               dur={`${props.defaultDurTime}ms`}               fill="freeze"               from={allLen.value}               to={progressOffset.value}             ></animate>           </rect>         )}       </svg>     );   };   watchEffect(() => {     allLen.value = (props.width + props.height) * 2 - props.radian * 8 + 2 * props.radian * Math.PI;     if (props.strokeLinecap !== 'butt' && props.progress !== 100 && props.progress > 95) {       progressOffset.value         allLen.value - (props.progress / 100) * allLen.value + props.strokeWidth;     } else {       progressOffset.value = allLen.value - (props.progress / 100) * allLen.value;     }     if (Object.prototype.toString.call(props.color) === '[object Object]') {       strokeBgColor.value = `url(#progressGradient)`;     } else {       strokeBgColor.value = props.color;     }     handleSave();   }); </script> 把这段代码使用vue3实现
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值