views:

374

answers:

1

I want the validator for password text input.

  • At least one Upper case letter
  • At least one numeric character
  • 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.

+3  A: 

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>
Amarghosh
Thank you.it is helpfull for me
praveen
Added the code to do it using flex's `RegExpValidator` class. Btw, if you are new to SO, you might wanna read this http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about
Amarghosh
If you wanna check with a single regex, use `/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/` Thanks @Bart for this. See http://stackoverflow.com/questions/1559751 for a detailed explanation.
Amarghosh