flex 正则验证密码强度 必须包含 大小写字符,数字,符号

xiaoxiao2021-03-01  4

验证代码片段:

 

private function isValidPassword(value:String):Boolean { if(value.length < minLength) return false; var regexArray:Array = [/[A-Z]/, /\d/, /[a-z]/, /[^\w]/]; var flag:Boolean = true; var result:Object; for(var i:Number = 0; i < regexArray.length; i++) { result = RegExp(regexArray[i]).exec(value); if(!result) { trace(value + " fails test " + i); return false; } } return true; }

 

 

1. /A-Z/ 验证至少一个大写字母

2. /a-z/ 验证至少一个小写字母

3. /\d/ 验证至少一个数字

4. /[^\w] 验证至少一个非单词字符, 注意:  正则将 _ (下划线)计算为一个字符

补充: 如果需要把_(下划线)包含进去,正则就应该这样写: /[^\w]|_/

 

 

下面是一个完整的例子:

 

<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.events.ValidationResultEvent; import mx.validators.RegExpValidationResult; private var validators:Array; protected function button2_clickHandler(event:MouseEvent):void { if(str5.validate().type!=ValidationResultEvent.VALID){ Alert.show(str5.validate().message); return ; }else{ for(var i:Number = 0; i < validators.length; i++){ var vs:ValidationResultEvent = RegExpValidator(validators[i]).validate(); if(vs.type != ValidationResultEvent.VALID){ Alert.show(vs.message); return; } } } Alert.show("验证通过!"); } protected function application1_creationCompleteHandler(event:FlexEvent):void { validators = [reg1, reg2, reg3, reg4]; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <mx:RegExpValidator id="reg1" source="{pwd}" expression="[a-z]" noMatchError="至少包含一个小写字母" property="text"/> <mx:RegExpValidator id="reg2" source="{pwd}" expression="[A-Z]" noMatchError="至少包含一个大写字母" property="text"/> <mx:RegExpValidator id="reg3" source="{pwd}" expression="\d" noMatchError="至少包含一个数字" property="text"/> <mx:RegExpValidator id="reg4" source="{pwd}" expression="[^\w]|_" noMatchError="至少包含一个符号" property="text"/> <mx:StringValidator id="str5" source="{pwd}" property="text" required="true" requiredFieldError="不能为空" minLength="10" tooShortError="至少10个字符"/> </fx:Declarations> <s:TextInput x="61" y="111" width="258" id="pwd"/> <s:Button x="362" y="112" label="验证" click="button2_clickHandler(event)"/> </s:Application>

 

相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-3850245.html

最新回复(0)