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

ysantana
ysantana
9,579 Points

Add Special Character validation to password lookahead RegEX

Hi,

I believe special characters should also be part of the password validation, since in must web forms special characters are required.

I posted some options to accomplish this in the answers. Happy to see other solutions :)

1 Answer

ysantana
ysantana
9,579 Points

Option 1: Match a string that contains a lowercase, uppercase, number and any non-word character:

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

Option 2: Match a string that contains a lowercase, uppercase, number and any of the special characters on regex:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*['@', '$', '#']).*$/.test(string);