user1809790
user1809790

Reputation: 1369

Password Regex Issue

I need a regex to validate a password input by the user through JS in an online registration form. The criteria for the password is the following:

  1. has to be longer than 8 characters
  2. has to include at least 1 number, 1 lower case letter and 1 upper case letter
  3. cannot include curly brackets {}
  4. must allow any other special characters (like @, #, $, %, _, " etc.)
  5. the order of the number, lowercase letters and upper case letters can be random, so it can be for example Ab1394*" or *"bA1394

I have tried to use the following regex, however if you input any special character such as '?' before the curly bracket {, it would allow it as a valid input. I am new to using regex and not really familiar so might be there's something minor in it:

^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[,#?!=@%&\^\$\*\)\(\_\.\'\"\+\-]).{8,}$

Can someone help me with a regex for this or point any issues in mine? Thanks

Upvotes: 1

Views: 426

Answers (1)

Thomas Ayoub
Thomas Ayoub

Reputation: 29441

To easily forbid characters, change .{8,} to [^{}]{8,}:

^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*[,#?!=@%&\^\$\*\)\(\_\.\'\"\+\-])[^{}]{8,}$

Or include any authorized char within the class:

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

Upvotes: 3

Related Questions