1
我想驗證密碼文本輸入。如何在flex中提供密碼驗證
- 至少有一個大寫字母
- 至少有一個數字字符
- 至少一個特殊字符,如@,#,$等
應該有在密碼如何我可以給它在行動腳本或mxml.please幫助我。 謝謝。
我想驗證密碼文本輸入。如何在flex中提供密碼驗證
應該有在密碼如何我可以給它在行動腳本或mxml.please幫助我。 謝謝。
你可以使用正則表達式。
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>
謝謝you.it是有益的,我 – praveen 2009-10-12 10:37:49
添加的代碼使用Flex的'RegExpValidator'類做一個Flex應用程序。順便說一句,如果你是新來的SO,你可能想讀這個http://meta.stackexchange.com/questions/5234/accepting-answers-what-is-it-all-about – Amarghosh 2009-10-12 11:24:14
如果你想檢查一個正則表達式,使用'/^(?=.*[az])(?=.*[AZ])(?=.*\d)(?=.*(?|[^\w])).+$/ '謝謝@Bart爲此。有關詳細說明,請參閱http://stackoverflow.com/questions/1559751。 – Amarghosh 2009-10-14 08:28:50