1

Hope you're fine

I would like to have a regex that could give me a password including these specifications :

  • numerics
  • alphabets ( Uppercase and Lowercase )
  • 8 words at least

thanks you in advance !

Amos M
  • 11
  • 2
  • 8 _words_? Did you mean characters? – user1280483 Nov 28 '21 at 02:49
  • Regex will not give you a random string, it just checks the given string and returns the matched results. If you want to verify a given string includes your requirements you can use the @steve_pineapple 's answer. If you want to generate a password, you can use this answer: https://stackoverflow.com/a/16548229/5793132 – Ugur Eren Nov 28 '21 at 03:07

1 Answers1

1

You can use the regular expression (?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,}) to validate if...

  • the password is 8 or more characters long ((?=.{8,})),
  • if the password has at least one uppercase letter ((?=.*[A-Z])),
  • if the password has at least one lowercase letter ((?=.*[a-z])) and
  • contains at least one digit ((?=.*[0-9])).

The following function in JavaScript shows how you can use the regular expression to check if a password meets the requirements.You didn't mention what language you where using, but it should work in a other langauges (it worked in Python, Ruby, PHP, & Java).

function validate_password(password) {
  let check = /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/;
  if (password.match(check)) {
     console.log("Your password is strong.");
  } else {
    console.log("Meh, not so much.");
  }
}

validate_password("Password123"); // strong password
validate_password("OtherPassword"); // no numbers
validate_password("password123"); // no uppercase
validate_password("ToShort"); // to short

this website has more details on password strength checking.

steve_pineapple
  • 107
  • 1
  • 9
  • Such criteria of forcing a lower case, a upper case, a number and a symbol (or some combination of those) for passwords is obsolete and deprecated a few years ago, in favor for longer passwords, ideally randomly generated. In your example, what you deem a as strong password is, in fact, a **TERRIBLE** one, very likely to be cracked within seconds. – Alejandro Nov 28 '21 at 04:41