Blini
Blini

Reputation: 15

HTML5 input pattern validate letters and numbers

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>

Upvotes: 0

Views: 4034

Answers (1)

Lit
Lit

Reputation: 114

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>

Upvotes: 1

Related Questions