Categories
JavaScript Answers

How to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase?

Spread the love

Sometimes, we want to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.

In this article, we’ll look at how to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.

How to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase?

To create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase, we can create a regex with all the patterns in it.

For instance, we write

const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/;

to define the regex that matches all the patterns.

(?=.*\d) matches at least one digit.

(?=.*[a-z]) matches at least one lower case.

(?=.*[A-Z]) matches at least one upper case.

[a-zA-Z0-9]{8,} matches at least 8 from the mentioned characters.

Conclusion

To create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase, we can create a regex with all the patterns in it.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.