Requires Flex Regex Validation Expression for password, which only accepts alphanumeric values ​​and requires one number

Hi I am trying to set up a RegexpValidator that only accepts a 6 to 30 character string of alphanumeric characters and requires one number. I'm new to regex and everything I've tried seems to keep returning an invalid ValidationRsultEvent. Here's a piece of code:
<mx:RegExpValidator id="regexValidator" source="{passwordInput}" property="text"
                        triggerEvent="" valid="onPasswordValidate(event)" invalid="onPasswordValidate(event)" />

private function validateRegister():void
{
  regexValidator.expression = "^(?=.*(\d|\W)).{6,30}$";
  regexValidator.validate();
}

      

+2


a source to share


2 answers


I'm not sure what would cause the validation error, but how far does your regex go to match alphanumeric strings with at least one try number ^(?=.*\d)\w{6,30}$

^         # Match begining of string
(?=.*\d)  # Lookahead, assert there is any number of characters followed by a digit
\w{6,30}  # \w matches letters, digits and the underscore character, 6-30 of them
$         # Match End of string

      



If you only want to use letters and numbers, \w

you can use instead [0-9a-zA-Z]

.

Your current regex ^(?=.*(\d|\W)).{6,30}$

matches any string that contains at least one character other than [a-zA-Z_]

( \d|\W

matches a character or a no-word character) between 6 and 30 characters in length, which doesn't necessarily match the requirements you specify.

+3


a source


According to the ActionScript manual, the backslash is a reserved character. Therefore your expression should look like



"^(?=.*(\\d|\\W)).{6,30}$"

      

+1


a source







All Articles