0

What regex can pass all of these password patterns

At least 1 digit - Mandatory
8 characters long - Mandatory

and the password also may include (optional)

one or more uppercase or lowercase
one ore more special character

I would want both of these 2 password pattern pass

  1. Password@2020 / password@2020
  2. Password2020 / password2020

EDIT

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}|$

This works for the second pattern i.e Password2020 / password2020 but what if I would want to allow the user to OPTIONALLY add a special character like Password@2020 / password@2020

  • See https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a – The fourth bird May 10 '20 at 14:04
  • Use let myregex = /[Pp]assword@?2020/ – Nick May 10 '20 at 14:07
  • 1
    What have you tried? What didn't work? What did you get? What did you expect? What doesn't work with your code and where is it? – Toto May 10 '20 at 14:21
  • ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}|$ this works for password2020 and Password2020, the second option but what if I would want to allow the user to add one or more special characters to their password, not compulsory but still would want to give them an option – Manish Bathla May 10 '20 at 14:52
  • 1
    Thd question is too unclear, judging by the rules you just need `/^(?=.{8})\D*\d/` - at least eight chars and at least one digit. – Wiktor Stribiżew May 10 '20 at 15:11
  • @WiktorStribiżew, can we optionally allow the user to add special character to their password, something like password@2020, here @ (the special character) should be optional. Freedom to choose password2020 or password@2020. A regex that can satisfy both these conditions? – Manish Bathla May 10 '20 at 15:21
  • My above regex allows any characters in the input. Please explain the condition you wang to add in words. Please edit the question. – Wiktor Stribiżew May 10 '20 at 15:42
  • When you make something optional, then there's no need to encode that in the regex, by definition. Wiktor's regex does it, based on the question as asked. – ggorlen May 10 '20 at 15:50

0 Answers0