33

can any one help me in creating a regular expression for password validation.

The Condition is "Password must contain 8 characters and at least one number, one letter and one unique character such as !#$%&? "

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Andromeda
  • 12,659
  • 20
  • 77
  • 103
  • 1
    Password rules are bad. Please see [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation) for more info. – ctwheels Jan 29 '18 at 17:18

6 Answers6

84
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$

---

^.*              : Start
(?=.{8,})        : Length
(?=.*[a-zA-Z])   : Letters
(?=.*\d)         : Digits
(?=.*[!#$%&? "]) : Special characters
.*$              : End
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • +1 for explanation - tested with a few examples and works at http://www.regular-expressions.info/javascriptexample.html – amelvin Mar 03 '10 at 09:44
  • i tired with 'acf23!&7h' and its not validating it – Andromeda Mar 03 '10 at 09:56
  • 9
    You don't need to put .* at the beginning and end of the match. It's pointless, and will cause significantly worse run times (especially in the case of invalid matches). – Tom Lord Aug 12 '13 at 08:48
  • How do I put a `length` limit on this regex? – lbrahim Jun 13 '14 at 13:06
  • Would (?=.*\W|_) work better for symbols if you want to include basically every symbol? – Steve Hiner Mar 27 '18 at 22:16
  • Using the suggestion from @Tom Lord I modified `^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$%^&-+=()!? "]).{8,128}$` which matches a password containing atleast 1 lower case letter, 1 upper case letter, 1 digit and one of the mentioned special characters and which must be between 8 to 128 characters in length. – Jignesh Gohel Dec 28 '20 at 14:10
14

Try this

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})

Description of above Regular Expression:

(           # Start of group
  (?=.*\d)      #   must contains one digit from 0-9
  (?=.*[a-z])       #   must contains one lowercase characters
  (?=.*[\W])        #   must contains at least one special character
              .     #     match anything with previous condition checking
                {8,20}  #        length at least 8 characters and maximum of 20 
)           # End of group

"/W" will increase the range of characters that can be used for password and pit can be more safe.

Rohit Dubey
  • 1,234
  • 15
  • 15
  • 1
    Why do you need all of the (.*)'s? Seems to work fine if you just did: (?=\d*)(?=[a-z]*)(?=[A-Z]*)(?=[\W]*).{6,20} – Nick George Apr 24 '14 at 15:49
7

You can achieve each of the individual requirements easily enough (e.g. minimum 8 characters: .{8,} will match 8 or more characters).

To combine them you can use "positive lookahead" to apply multiple sub-expressions to the same content. Something like (?=.*\d.*).{8,} to match one (or more) digits with lookahead, and 8 or more characters.

So:

(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#\$%&\?].*).{8,}

Remembering to escape meta-characters.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • 1
    You have a few pointless ".*"s in there. You could just use: (?=.*\d)(?=.*[a-zA-Z])(?=.*[!#\$%&\?]).{8,} – Tom Lord Aug 12 '13 at 08:51
  • @TomLord I would want to test that… but you may well be right (reason: of course each positive lookahead only needs to confirm as far as one instance of each type of character). – Richard Aug 19 '13 at 05:11
  • Yes, and the main reason I pointed this out is that if your regex does NOT match then it may be made vastly more inefficient to determine this if you include unnecessary ".*"s in there! – Tom Lord Aug 23 '13 at 09:22
6

Password with the following conditions:

  1. At least 1 digit
  2. At least 2 special characters
  3. At least 1 alphabetic character
  4. No blank space

    'use strict';
    (function() {
        var foo = '3g^g$';
    
        console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo));
    
        /**
         * (?=.*\d)         should contain at least 1 digit
         * (?=(.*\W){2})    should contain at least 2 special characters
         * (?=.*[a-zA-Z])   should contain at least 1 alphabetic character
         * (?!.*\s)         should not contain any blank space
         */
    })();
    
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
1

You can make your own regular expression for javascript validations;

        (/^
        (?=.*\d)                //should contain at least one digit
        (?=.*[a-z])             //should contain at least one lower case
        (?=.*[A-Z])             //should contain at least one upper case
        [a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters

        $/)

Example:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/

Irshad
  • 3,071
  • 5
  • 30
  • 51
Ujjaval
  • 447
  • 5
  • 10
0
var regularExpression = new RegExp("^^(?=.*[A-Z]{"+minUpperCase+",})" +
    "(?=.*[a-z]{"+minLowerCase+",})(?=.*[0-9]{"+minNumerics+",})" +
    "(?=.*[!@#$\-_?.:{]{"+minSpecialChars+",})" +
    "[a-zA-Z0-9!@#$\-_?.:{]{"+minLength+","+maxLength+"}$");
if (pswd.match(regularExpression)) {
    //Success
}