Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how do i recreate a password requirements regex? #198

Open
majkl-zumberi opened this issue Feb 8, 2023 · 5 comments
Open

how do i recreate a password requirements regex? #198

majkl-zumberi opened this issue Feb 8, 2023 · 5 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@majkl-zumberi
Copy link

📚 Is your documentation request related to a problem?

i'm trying to recreate a password requirements regex like the following:
password must contains al least 8 characters, one lowercase character, one uppercase char, one digit and one special character (! @ # $ % ^ & *)

const PASSWORD_RE= createRegExp(
  (
    oneOrMore(letter.lowercase).times.atLeast(1)
  .and(oneOrMore(letter.uppercase).times.atLeast(1))
  .and(oneOrMore(digit))
  .and(
    oneOrMore(
      charIn('!')
      .or(charIn('@'))
      .or(charIn('#'))
      .or(charIn('$'))
      .or(charIn('%'))
      .or(charIn('^'))
      .or(charIn('&'))
      .or(charIn('*'))
      ).times.atLeast(1)
    )
  )
.times.atLeast(8),['g']
)

i tried to paste the generated regex in one of the regex tester like regex101
but seems to not working as expected

the documentation seems lacking in terms of providing a fully understandable examples

can you help me to achieve a working password regex? Where am I doing wrong?

🔍 Where should you find it?

No response

ℹ️ Additional context

No response

@majkl-zumberi majkl-zumberi added the documentation Improvements or additions to documentation label Feb 8, 2023
@majkl-zumberi majkl-zumberi changed the title how do i recreate a password requirement regex? how do i recreate a password requirements regex? Feb 8, 2023
@didavid61202
Copy link
Collaborator

You can do something like this:

const PASSWORD_RE = createRegExp(
  exactly('')
    .before(char.times.any().and(letter.lowercase))
    .before(char.times.any().and(letter.uppercase))
    .before(char.times.any().and(digit))
    .before(char.times.any().and(charIn('!@#$%^&*')))
    .and(letter.or(digit.or(charIn('!@#$%^&*'))).times.atLeast(8))
    .at.lineStart()
    .at.lineEnd()
)

charIn can just take all characters in one go as a string, but we do still missing Input helper that create custom range(s) to simplify more, ex: for regexp [b-t3-6]

@majkl-zumberi
Copy link
Author

thanks @didavid61202 for your solution, i'll end up closing the issue
but let me first ask you to briefly explain how you ended up with this solution
why there's the need of exactly('')? and .at.lineStart() .at.lineEnd() what they do?

the documentation as i mentioned before isn't that clear

@leofcshen
Copy link

leofcshen commented Feb 15, 2023

@didavid61202
your solution create regex ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%\^&*])(?:[a-zA-Z]|(?:\d|[!@#$%\^&*])){8,}$
how to create more clear regex like ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%\^&*]).{8,}$

@toindev
Copy link

toindev commented Mar 6, 2023

Hi, just as an aside, "hard" password requirements are actually counter-productive, as a security measure.

For the purpose of checking the safety of a password, you would be better served by something like https://github.com/dropbox/zxcvbn

Nonetheless, have fun with regular expressions!

@zoeyzhao19
Copy link
Contributor

zoeyzhao19 commented Jun 21, 2023

@didavid61202 your solution create regex ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%\^&*])(?:[a-zA-Z]|(?:\d|[!@#$%\^&*])){8,}$ how to create more clear regex like ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%\^&*]).{8,}$

maybe

const regexp = createRegExp(
    exactly(
      exactly('')
        .before(char.times.any().and(letter.lowercase))
        .before(char.times.any().and(letter.uppercase))
        .before(char.times.any().and(digit))
        .before(char.times.any().and(charIn('!@#$%^&*'))),
      char
        .times.atLeast(8),
    )
      .at.lineStart()
      .at.lineEnd()
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

6 participants