0

I want to use a regex to match a set of password requirements. They are:

  • Needs to 8-15 characters
  • One uppercase letter
  • One lowercase letter
  • One number

I'm using https://regexr.com/ to try to build it.

The first attempt:

/([A-Z])([a-z])([0-9])({8,15})+/g

But in the console of the web app this throws:

enter image description here

But in this answer, Regular expression to limit number of characters to 10 they go:

/^[a-z]{0,10}$/

So I tried to roll with this idea:

/[A-Z][a-z][0-9]{8,15}/g

And the errors clear but it is always false. Why?

Below is a sample. The alert should be true because it contains all the requirements.

function checkPass() {
  var regex = /[A-Z][a-z][0-9]{8,15}/g;
  var string = "G00dPassword1234"
  var isItGood = regex.test(string);
  alert(isItGood)
}
<button onclick="checkPass()">test</button>

Why is my test always false?

kawnah
  • 3,204
  • 8
  • 53
  • 103
  • 2
    `^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,15}$` or `^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[A-Za-z0-9]{8,15}$` – Wiktor Stribiżew Jun 13 '19 at 19:38
  • 1
    The regex you got means one uppercase A-Z, followed by one lowercase a-z, followed by 8 to 15 times a digit. See [this](https://jex.im/regulex/#!flags=&re=%5BA-Z%5D%5Ba-z%5D%5B0-9%5D%7B8%2C15%7D) for a visualization. – Ivar Jun 13 '19 at 19:40

0 Answers0