Validation
Strong Password Rules Regex
Many sign-up forms enforce composition rules for passwords. This pattern uses four lookaheads at the start of the string, and each one scans ahead to confirm that a lowercase letter, an uppercase letter, a digit and a non-alphanumeric symbol appear somewhere. Lookaheads do not consume characters, so after all four checks pass, the final part simply demands at least twelve characters of anything. Because each rule is a separate lookahead, you can delete or change one rule without touching the others. Showing users which rule failed is friendlier than one combined error, so many teams also run each lookahead as its own small check.
Pattern (JavaScript)
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$/Starts at the start of the line/string, then a position followed by (any character, zero or more times, then a lowercase letter), then a position followed by (any character, zero or more times, then an uppercase letter), then a position followed by (any character, zero or more times, then a digit (0-9)), then a position followed by (any character, zero or more times, then any character that is not a letter or digit), then any character, 12 or more times, then the end of the line/string.
Limit: Current guidance (e.g. NIST SP 800-63B) favours length over composition rules; treat these rules as optional.
Examples
Matches
- ✓ Correct-Horse9battery
- ✓ Ab1!Ab1!Ab1!
- ✓ Sara.Demo2026x
Does not match
- ✗ Short1A!
- ✗ alllowercase123!
- ✗ NoDigitsHere!!x
Try it
0 matches (JavaScript engine)
Correct-Horse9battery
Ab1!Ab1!Ab1!
Sara.Demo2026x
Short1A!
alllowercase123!
NoDigitsHere!!xCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$/g;
const text = "Correct-Horse9battery";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$')
text = 'Correct-Horse9battery'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$/';
$text = 'Correct-Horse9battery';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^A-Za-z0-9]).{12,}$");
String text = "Correct-Horse9battery";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
ℹ Go's RE2 engine has no lookahead. This checks length only; run the other rules as separate checks in code.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^.{12,}$`)
text := "Correct-Horse9battery"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$");
var text = "Correct-Horse9battery";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
ℹ The Rust regex crate has no lookahead. This checks length only; run the other rules as separate checks in code.
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^.{12,}$").unwrap();
let text = "Correct-Horse9battery";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$/
text = 'Correct-Horse9battery'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why use lookaheads instead of one long pattern?
- Without lookaheads you would need to list every order in which the character types could appear. Each lookahead checks one rule independently, so the order of characters does not matter.
- Are composition rules still recommended?
- Current guidance such as NIST SP 800-63B favours longer passwords and checks against known breached passwords over composition rules. Treat the four character-type rules as optional and keep the length requirement.
- Does a space count as a symbol here?
- Yes. The symbol lookahead accepts any character that is not a letter or digit, which includes spaces. That lets users pick passphrases made of several words.