splitforms.com
Free tool · runs in your browser

Password Regex Generator

Pick the rules a password must follow, test sample passwords as you type, and copy the JavaScript regex or a ready <input type="password"> with the pattern attribute. Nothing you type leaves your browser.

Presets
Must contain
JavaScript regex
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/u
HTML input (no ^ or $)
<label for="password">Password</label>
<input type="password" id="password" name="password"
       autocomplete="new-password"
       pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}"
       title="At least 8 characters, with a lowercase letter, an uppercase letter and a digit" required>

Runs only in your browser. Don't type a password you really use.

  • At least 8 characters fails
  • A lowercase letter fails
  • An uppercase letter fails
  • A digit fails

Empty: the regex fails (the HTML pattern skips empty fields; required catches them).

How each lookahead works

A lookahead (?=...) checks that something appears somewhere in the password without moving forward, so the rules stack in any order. This is the Strong preset with No spaces ticked:

PartWhat it does
^Start of the password.
(?=.*[a-z])Looks ahead for at least one lowercase letter a-z.
(?=.*[A-Z])Looks ahead for at least one uppercase letter A-Z.
(?=.*\d)Looks ahead for at least one digit 0-9.
(?=.*[^A-Za-z0-9])Looks ahead for at least one character that is not an ASCII letter or digit, such as ! or #. A space or an accented letter like é also counts.
(?!.*\s)Negative lookahead: fails if the password contains a space, tab or other whitespace.
.{12,}Consumes the whole password: at least 12 characters.
$End of the password, so nothing extra can follow.

Use it in HTML, JavaScript or React

The snippets use the Strong preset. In HTML and React, paste the pattern without ^ and $; in JavaScript, keep the anchors and leave off the g flag.

HTML: the pattern attribute

password.html

<label for="password">Password</label>
<input type="password" id="password" name="password"
       autocomplete="new-password"
       pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}"
       title="At least 12 characters, with a lowercase letter, an uppercase letter, a digit and a symbol" required>

JavaScript: test before you submit

password.js

// No g flag: a global regex keeps lastIndex between .test() calls.
const PASSWORD_RE = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$/u;

PASSWORD_RE.test("Abcdef1!xyz9"); // true
PASSWORD_RE.test("abc");          // false

React: the same pattern as a prop

PasswordField.jsx

export function PasswordField() {
  return (
    <>
      <label htmlFor="password">Password</label>
      <input
        type="password"
        id="password"
        name="password"
        autoComplete="new-password"
        pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}"
        title="At least 12 characters, with a lowercase letter, an uppercase letter, a digit and a symbol"
        required
      />
    </>
  );
}

Whatever the browser checks, check it again on the server; see server-side form validation.

Password regex FAQ

What is a good regex for password validation?

For 12 or more characters with a lowercase letter, an uppercase letter, a digit and a symbol, use ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$. Each (?=...) lookahead checks one rule without consuming characters, and the final .{12,} checks the length. Drop a lookahead to drop that rule.

Why does the HTML pattern have no ^ or $?

The browser anchors the pattern attribute for you: it wraps it as ^(?: and )$ and compiles it with the v flag, so it must match the whole value. Leaving ^ and $ out keeps the pattern short; adding them does no harm.

Is a password regex in the browser enough security?

No. The pattern attribute and JavaScript checks only help honest visitors fix a password before they submit. Anyone can turn them off or post straight to your server, so the server must check the password again, then store only a salted hash made with a slow algorithm such as Argon2, bcrypt or scrypt.

Should I require uppercase letters, digits and symbols?

NIST SP 800-63B advises against composition rules like these and favours length: it tells verifiers not to require mixes of character types, to allow at least 64 characters, and to check new passwords against lists of known breached or common passwords. The NIST-style preset follows that with a length-only rule.

Does the symbol rule count spaces and accented letters?

Yes. [^A-Za-z0-9] matches any character that is not an ASCII letter or digit, so a space, é or an emoji satisfies it. Tick No spaces if you want to block whitespace, and remember that a regex can't tell a strong password from a common one like Password1!.

Source: NIST SP 800-63B, Digital Identity Guidelines.

Related