0

I'm new to React Native, and I need to implement new password requirements.

The new requirements are small and large letters or letters and at least one number or special character. The requirement for the password to be at least eight characters.

Here is my code:

.matches(
      /^(?=.*[a-z])(?=.*\d)(?=.*[\W_])[\w\W].+$/,
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manko
  • 51
  • 1
  • 8
  • 3
    Does this answer your question? [Regular Expression for password validation](https://stackoverflow.com/questions/2370015/regular-expression-for-password-validation) You can find many more possible solutions by searching this site for `[regex] password validation 8` using the Search bar at the top of the page. – Ken White Nov 12 '21 at 18:53
  • I need 'OR CONDITION' small and large letters OR letters and at least one number OR special character – Manko Nov 12 '21 at 19:05
  • So did you search using the search phrase I gave you (so you don't even have to write one) to look at the many other similar questions? I see at least a dozen on the first page of results that may be exactly what you're looking for, if you bothered to go and read them. – Ken White Nov 12 '21 at 19:16
  • i didn't find my answer – Manko Nov 12 '21 at 19:26

2 Answers2

0

I think that should work:

((^|, )((?=.[a-z])|(?=.\d)|(?=.*[\W_])[\w\W]))+$.

samuelnehool
  • 147
  • 15
0

This works. It requires at least one uppercase letter, one lowercase letter, one number, and one special character such as @ or # or $, with a length of at least eight characters.

(?m)^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\W]).{8,})$

The (?m) at the beginning makes sure that the . in the regex does not match a newline.

From RegexBuddy:

  • ^((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[\W]).{8,})$

  • Options: ^ and $ match at line breaks

  • Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»

  • Match the regular expression below and capture its match into backreference number 1 «((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[\W]).{8,})»

    • Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*\d)»
      • Match any single character that is not a line break character «.*»
        • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      • Match a single digit 0..9 «\d»
  • Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[a-z])»

    • Match any single character that is not a line break character «.*»
      • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • Match a single character in the range between “a” and “z” «[a-z]»
  • Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[A-Z])»

    • Match any single character that is not a line break character «.*»
      • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • Match a single character in the range between “A” and “Z” «[A-Z]»
  • Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[\W])»

    • Match any single character that is not a line break character «.*»
      • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • Match a single character that is a “non-word character” «[\W]»
    • Match any single character that is not a line break character «.{8,}»
    • Between eight and unlimited times, as many times as possible, giving back as needed (greedy) «{8,}»
  • Assert position at the end of a line (at the end of the string or before a line break character) «$»

It matches

abcDefg1$
1zBA^frmb
1#Basdfadsfadsf

It does not match

abcd123
123abc
abcdEFGH
abcdEFG2
abCDeF1E
1a2bc
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ken White
  • 123,280
  • 14
  • 225
  • 444