0

I need to validate password with these requirements:

At least 1 Letter, At least 1 Number, Min 6 chars and Max 12 chars, Special characters not allowed. Here's what I have so far.

<form>
Password: <input type="password" name="pw" pattern="^(?=.*[a-zA-Z])(?=.*[0-9]).{6,12}$" title="Must contain at least one number and one letter, and at least 6 to 12 characters (special characters not allowed)">
<input type="submit">
</form>
2
  • Special characters not allowed - then why use .? Replace .{6,12} with [a-zA-Z0-9]{6,12} if you plan to only match ASCII letters and digits. Commented May 2, 2018 at 10:51
  • Reference - Password Validation may really benefit you Commented May 4, 2018 at 14:50

1 Answer 1

1

Below is regex that solves your problem.

^(?=.*[a-zA-Z])(?=\w*[0-9])\w{6,12}$

Full answer is:

<form>
    Password: <input type="password" name="pw" pattern="^(?=.*[a-zA-Z])(?=\w*[0-9])\w{6,12}$" title="Must contain at least one number and one letter, and at least 6 to 12 characters (special characters not allowed)">
    <input type="submit">
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to allow one specific character like Space or . (Dot)? pattern="^(?=.*[a-zA-Z\s])(?=\w*[0-9])\w{6,12}$" *( "_" underscore is not allowed for me)
^(?=\w*[a-zA-Z])(?=\w*[0-9])[A-Za-z0-9 \.]{6,12}$

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.