1

I have a JavaScript regular expression that I'm using for frontend password validation in an input field. Here is what my input field looks like:

<input type='password' placeholder='Password...' id='password' pattern='^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})' required>

That regular expression is looking for a string: eight characters or longer, with an uppercase letter, lowercase letter, number, and a special character. Let's take the string: November2017! for instance. That should pass the regular expression, but it doesn't in my webapp. And when I copy and paste the regular expression from my input field ^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,}) and put it into regex101.com, that string passes the validation. But it does not pass the validation in my webapp. Is there a specific reason why putting it in a pattern attribute would change whether or not it would pass validation?

3
  • Regex isn't 100% standardized, are you sure the regex tester was set to JavaScript's regex? Commented Nov 15, 2017 at 2:57
  • @Gry- yes, I checked it on the JavaScript regex Commented Nov 15, 2017 at 2:58
  • pattern requires to match the complete string. Your pattern does not match the complete string regex101.com/r/WcqXSR/1 It matches the position in the beginning of the string only. Commented Nov 15, 2017 at 3:01

1 Answer 1

2

You need to add the .{8,} in the end.

^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*]).{8,}

It happens because the pattern must match the complete string, and your current expression only matches the beginning of the string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.