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>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Blini
  • 15
  • 1
  • 3
  • *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. – Wiktor Stribiżew May 02 '18 at 10:51
  • [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/48346033#48346033) may really benefit you – ctwheels May 04 '18 at 14:50

1 Answers1

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>
Lit
  • 116
  • 5
  • 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) – Blini May 02 '18 at 12:28
  • ^(?=\w*[a-zA-Z])(?=\w*[0-9])[A-Za-z0-9 \.]{6,12}$ – PRMan Jul 20 '22 at 18:01