2

Working on a project where it requires me to have a password field using pattern attribute. Not really done a lot of regex stuff and was wondering whether someone could help out.

The requirements for the field are as follows:

  • Can't contain the word "password"
  • Must be 8-12 in length
  • Must have 1 upper case
  • Must have 1 lower case
  • Must have 1 digit

Now, so far I have the following:

[^(password)].(?=.*[0-9])?=.*[a-zA-Z]).{8,12}

This doesn't work. We can get it so everything else works, apart from the password string being matched.

Thanks in advance, Andy

EDIT: the method we've used now (nested in comments below) is: ^(?!.*(P|p)(A|a)(S|s)(S|s)(W|w)(O|o)(R|r)(D|d)).(?=.*\d)(?=.*[a-zA-Z]).{8,12}$

Thanks for the help

3
  • I tried all the ones that I could find on stackoverflow and couldn't find one that worked correct for html5 Commented Oct 25, 2013 at 10:03
  • 1
    Why do your requirements list a maximum of 12 characters? This isn't the 1960s anymore, most hardware/software today can handle longer passwords. explainxkcd.com/wiki/index.php?title=936:_Password_Strength Commented Oct 25, 2013 at 10:24
  • It's just in the spec for what the company requested, not my business to advice and things like that unfortuneately Commented Oct 25, 2013 at 12:05

4 Answers 4

2

Use a series of anchored look aheads for the :must contain" criteria:

^(?!.*(?i)password)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}$

I've added the "ignore case" switch (?i) to the "password" requirement, so it will reject `the word no matter the case of the letters.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 just don't forget the $ at the end
For some reason I can't seem to get it to work for any case in html5
we've come up with this solution ^(?!.*(P|p)(A|a)(S|s)(S|s)(W|w)(O|o)(R|r)(D|d)).(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}$
Small edit, ^(?!.*(P|p)(A|a)(S|s)(S|s)(W|w)(O|o)(R|r)(D|d)).(?=.*\d)(?=.*[a-zA-Z]).{8,12}$ as the previous wasn't allowed capital at the start
1

This regex should to the job:

^(?!.*password)(?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,12}$

it will match:

^               // from the beginning of the input 
(?!.*password)  // negative lookbehind whether the text contains password
(?=.*\d+)       // positive lookahead for at least one digit
(?=.*[A-Z]+)    // positive lookahead for at least one uppercase letter
(?=.*[a-z]+)    // positive lookahead for at least one lowercase letter
.{8,12}         // length of the input is between 8 and 12 characters
$

Link to phpliveregex

9 Comments

This is similar to what I used, and this seemed to work great. Thanks!
This will match even if it contains PassWoRd, try using this at the front instead ^(?!.*((?i)password)) will match any cased 'password'
Look at my answer for simple solution !
We were just talking about this now, that you can use a different case for it. Will try this now. Thanks
why the plus signs after the character classes? it will work just as well without them
|
0

Try This:

^(?!.*password)(?=.*[A-Z])(?=.*[0-9])(?=.*[a-z]).{8,12}$

Explanation

^                         Start anchor
(?=.*[A-Z])               Ensure string has one uppercase letter
(?=.*[0-9])               Ensure string has one digits.
(?=.*[a-z])               Ensure string has one lowercase letter
{8,12}                    Ensure string is of length 8 - 12.
(?!.*password)            Not of word password
$                         End anchor.

1 Comment

your password check expression is pretty messed up: right now it means "must contain a character other than one of ()adoprsw"
0

Try this way

        Public void validation()
        string xxxx = "Aa1qqqqqq";
        if (xxxx.ToUpper().Contains("password") || !(xxxx.Length <= 12 & xxxx.Length >= 8) || !IsMatch(xxxx, "(?=.*[A-Z])") || !IsMatch(xxxx, "(?=.*[a-z])") || !IsMatch(xxxx, "(?=.*[0-9])"))
        {
             throw new System.ArgumentException("Your error message here", "Password");
        }
        }

        public bool IsMatch(string input, string pattern)
        {
          Match xx = Regex.Match(input, pattern);
          return xx.Success;
         }

2 Comments

We're using regex in html5
Same as I also used Regex , See that IsMatch method , My code helps for check your all requirement for password , if all is correct the if condition is return false, other wise that if condition will true, then add the error message there !You can check it !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.