Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Regular Expressions in JavaScript Validating a Form Validating a Password

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Define length for password

How to define length for password, That this should be at least the length of the password? I tried looking up online for this, but did not get it

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

can somebody explain this,

1 Answer

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

Hiya karan karan :wave:

This RegEx that you added to your post, is checking if there is at least one of each of a lowercase letter, uppercase letter and a number:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/

If you wanted to also add a minimum and maximum password length, you can use a Quantifier. For example if you wanted the password to have a minimum length of 8 characters and a maximum of 20, as well as still ensuring that there is at least one of each of a lowercase letter, uppercase letter and a number, you would change the RegEx to this:

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