2

I am using regex101 as a baseline for correct javascript regex. Here is my pattern:

/^(?=.*[0-9])(?=.*[a-zA-Z]).{8,40}/g

I am trying to create a password regex that is between 8-40 characters, with any characters but at least one number and at least one alphabet character.

On regex101, this pattern works as expected. asdf1234 correctly is matched.

However, trying to use this pattern in html5 form input pattern does not match the same results.

fiddle

What am I doing wrong?

1 Answer 1

2

Do not use the regex literal in HTML5 pattern attribute, it only accepts a string pattern (the / symbols on both ends in your pattern are treated as literal slashes).

<form>
    <input type="text" pattern="^(?=.*[0-9])(?=.*[a-zA-Z]).{8,40}$" />
    <input type="submit" value="OK" />
</form>

Note that the ^ and $ in this pattern are not required as the regex in HTML5 pattern is anchored by default, but are OK to use (it will be easier to debug using regex101 or similar service).

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

1 Comment

wonderful! Thank you! I'm surprised this isn't documented more in top search results - They don't use slashes in examples, but also don't call it out that they are not needed.

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.