我想要验证密码文本输入。
应该在密码中如何在操作脚本或mxml.please帮助我。 感谢。
答案 0 :(得分:3)
您可以使用正则表达式执行此操作。
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;
}
/A-Z/
检查大写字母。/a-z/
检查小写字母。/\d/
检查数字/[^\w]/
检查非单词字符。请注意,正则表达式将_视为单词字符。因此_不会被视为符号。更新:将/[^\w]|_/
添加到正则表达式数组以将_计为符号。
更新:这是使用RegExpValidator
的灵活应用<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
xmlns:local="*" creationComplete="create();">
<mx:TextInput id="textInput"/>
<mx:Button id="btn" label="Submit" click="validate();"/>
<mx:RegExpValidator id="reg1" source="{textInput}" expression="[a-z]"
noMatchError="At least one lower case char" invalid="onInvalid(event)"
property="text"/>
<mx:RegExpValidator id="reg2" source="{textInput}" expression="[A-Z]"
noMatchError="At least one upper case char" invalid="onInvalid(event)"
property="text"/>
<mx:RegExpValidator id="reg3" source="{textInput}" expression="\d"
noMatchError="At least one number" invalid="onInvalid(event)"
property="text"/>
<mx:RegExpValidator id="reg4" source="{textInput}" expression="[^\w]|_"
noMatchError="At least one symbol char" invalid="onInvalid(event)"
property="text"/>
<mx:Script>
<![CDATA[
import mx.events.ValidationResultEvent;
private var validators:Array;
public function create():void
{
validators = [reg1, reg2, reg3, reg4];
}
private function validate():void
{
for(var i:Number = 0; i < validators.length; i++)
if(RegExpValidator(validators[i]).validate().type != ValidationResultEvent.VALID)
return;
//valid
trace(textInput.text + " is valid");
}
private function onInvalid(e:Event):void
{
var validator:RegExpValidator = e.target as RegExpValidator;
trace(validator.noMatchError);
}
]]>
</mx:Script>
</mx:Application>