开发者

How can i give password validation in flex

I want the validator for password text input.

  • At least one Upper case letter
  • At least one numeric cha开发者_JAVA技巧racter
  • At least one special character such as @, #, $, etc.

should be there in password how can i give it in action script or mxml.please help me. Thanks.


You can do it using regex.

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/ checks for an upper case letter.
  2. /a-z/ checks for a lower case letter.
  3. /\d/ checks for a digit
  4. /[^\w]/ checks for a non word character. Note that regex treats _ as a word character. Thus _ will not be counted as symbol.

Update: Add /[^\w]|_/ to the regex array to count _ as symbol.

Update: Here is a flex app using 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜