tags:

views:

76

answers:

1

I am using Teleriks RadInputManager control to check that a password is between 7 and 16 characters and contains at least 1 numeric and 1 special character, but when I type something that I know fits the expression, validation fails, so I believe my regular expression is wrong. Here is the expression I am using:

/^(?=.{7,16}$)\D+\d/

I tried the following:

/^(?=.*\d)(?=.*[!@&.$#]).{7,16}$/

and tried to put the password test11. and it failed. I don't understand why because this is 7 characters and contains a numeric and a special character.

+1  A: 

Use lookaheads:

/^(?=.*\d)(?=.*[-;:]).{7,16}$/

You didn't specify what a special character is so I just used an example, but you'll have to replace it with something better.

Mark Byers
Thanks. I am new to regular expressions, but I am assuming anything between [ ] can be used as a special character, correct?
Xaisoft
Also, what exactly is a lookahead, I was looking at an indepth link that was explaining it before, but I got lost in translation, I just need a simple to understand example.
Xaisoft
Xaisoft: Yes any character can be written there, but you have to be careful with characters like `-`, `\` and `]` as these have a special meaning inside character classes and need to be escaped.
Mark Byers
Sorry that should have been: you have to be careful with: `-`, `\ ` and `]`. Apparently I need to be careful with these too.
Mark Byers
Mark, that was the link that I was actually looking at and although it was extremely helpful, I think because I don't have an understanding of doing it without a lookhead, I don't see the real value of a lookahead.
Xaisoft
@Xaisoft: Doing it in one regular expression without a lookahead would be crazy complicated. Using lookaheads allows you to test the three conditions separately instead of having to write one state machine that remembers the state of all three parts of the regular expressions simultaneously.
Mark Byers
Thanks, I changed the regular expression to /^(?=.*\d)(?=.*[.$#]).{7,16}$/. Note the special characters, is that the correct way to put it?
Xaisoft