Free · runs in your browser

Password Validation Regex Builder

Password Validation Regex Builder. Set your password rules to instantly build a matching validation regex plus ready-to-paste code. IT teams run the password policy validator against employees' sample inputs before turning on enforcement, which reduces reset tickets.

Your values
Length

Enter 0 for no upper limit.

Required characters

Used when special characters are required. Hyphens, carets, brackets and backslashes are escaped for you.

Optional JavaScript flags such as i (ignore case) or u (Unicode). g and y are ignored when testing.

Up to 2,000 lines or 100,000 characters. Spaces count as characters.

Result

Password regex
—
JavaScript code
—
Test result
—
Strings that match
—
Strings that don't match
—
What each part of the pattern does
—
Character classes required
—
Length
—
Total matching passwords
—
Sample passwords that pass
—
  • No account, no logging
  • Open formulas explained below
  • Works offline once loaded

Every sign-up form needs rules, and a password validation regex builder turns those rules into one regular expression you can paste into your code. You stack small checks for length, case, digits and symbols, then confirm the result with sample input. A pattern is only a first line of security, but it is the fastest protection a developer can add against weak choices and brute force attacks.

How a Password Validation Regex Builder Checks a Password

A password regex is a pattern that describes what an acceptable password looks like, and the engine reports whether a user's string fits. A regex pattern either does or does not match, so the answer is always yes or no, never a score. That makes it ideal for blocking short or one-sided passwords before they reach your database.

The trick is combining several independent rules. A single character class cannot say "needs a digit and a capital", so the pattern borrows an assertion for each rule and lets them all run at the same position.

Zero-Width Positive Lookahead Assertions

A positive lookahead is written (?=...). It peeks ahead from the current spot, answers true or false, and consumes nothing, which is why each assertion is called zero-width. Because nothing is consumed, you can chain as many lookahead assertions as you like at the start of the string, and the password only matches when every one of them holds. The anchors ^ (start of string) and $ (end of string) wrap the whole thing so the engine tests the full value, and a final .* lets the pattern actually consume it.

Building a Strong Password Regex Step by Step

Treat each requirement as one lookahead and add them in a fixed order. Here is the strong password regex assembled for this walkthrough, followed by what each part does: For a written rule set, the free password policy generator drafts a document covering length, complexity and reuse for your organization.

Diagram of a password validation regex split into four lookaheads for lowercase, uppercase, digit and special character plus a 12 to 64 length check
Each rule in the worked-example policy adds one fragment to the final password regex.

^(?=.{12,64}$)(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*_+=-]).*$

Building a Strong Password Regex Step by Step
RuleLookaheadMeaning
Length(?=.{12,64}$)12 to 64 characters in length
Lowercase(?=.*[a-z])at least one small letter
Uppercase(?=.*[A-Z])at least one capital letter
Digit(?=.*\d)at least one number
Symbol(?=.*[!@#$%^&*_+=-])at least one listed character

Minimum 8 Characters and Case Rules

Current NIST guidance sets a floor of minimum 8 characters, but longer is better, so this build asks for 12. The (?=.{12,64}$) lookahead sets the length, so change the numbers in {12,64} to move the limit. The next two checks require an uppercase letter and a lowercase letter; delete either lookahead if your policy does not need it, and the rest of the pattern keeps working.

Special Character and Password Complexity Rules

The last lookahead demands a special character from a list you control, plus the digit check from the table. Together these form the password complexity policy. Keep the symbol list explicit instead of using \W, because that shorthand also accepts spaces and accented letters you may not want. Whatever complexity validation rules you choose, show them to users before they type, since hidden rules cause the most abandoned forms. The same idea scales to regular expressions for other fields.

Using a Regex Tester for Password Strength Validation

Never ship a pattern you have not run. A regex tester highlights what matches and what fails as you type, so you can check password strength rules against real examples. Paste your pattern in one box and a test string in another; a good tester also reports capture groups. Choose Test it or an equivalent button and read the verdict. Here is what the pattern above returns for five sample values: Front-end developers can use the password regex tester to confirm that code in a JavaScript function matches expectations.

Grid of eight sample passwords tested against six rules, with only two passing every rule
Eight test passwords against six rules: each invalid string fails exactly one.
  • Kestrel#Moss41 (14 characters): valid, since it carries every required class.
  • harborlamp2024!: rejected, no capital letter.
  • HARBOR-LAMP-2024: rejected, no lowercase letter.
  • Tr0ub4dor&3: rejected, only 11 characters.
  • Ab1!: rejected, far too short.

If a value you expect to pass shows no highlight, one lookahead is rejecting it. Remove the assertions one at a time until the first match appears, and the last rule you removed is the culprit. A valid password should produce a full-string match from the first character to the last, so a partial highlight means the anchors are wrong. This habit also shows you which message to display beside the field: one specific hint such as "add a number" beats a generic error.

Note that a strict regex also measures password entropy poorly: "Kestrel#Moss41" passes, while a long lowercase passphrase would fail even though its entropy is higher. Test both kinds before you settle on the rules.

Form Validation in the Browser

Run the same lookahead chain on the page for instant feedback before submit, but remember that form validation in the browser is a convenience for the user, not a safeguard. A frontend check can be bypassed, so the server must re-run the pattern for real data validation. Keep privacy in mind as well: a client-side form that never sends the typed value anywhere is the safest way to demo a rule set.

Tuning a Password Regex Validation Rule for a Library Booking Form

I'm adding a sign-up form to a small library room-booking app, and the question is simple: should the minimum length be 10 or 14? I build the rule in a password validation regex builder with four lookaheads (length, lowercase, capital, digit) plus (?=\S+$) to forbid spaces, then paste four test strings into the tester.

Bar chart of possible random password combinations for lengths of 8, 10, 12, 14 and 16 characters
Possible random passwords grow by about four powers of ten for every two extra characters.
Tuning a Password Regex Validation Rule for a Library Booking Form
Test stringLengthMin 10Min 14
Quill7Basin11matchno match
quill7basin2213no match (no capital)no match
Quill Basin 7 Tower19no match (spaces)no match
Teal-Orchard-Ferry-5821matchmatch

The sensitivity rerun is the useful part: changing only {10,} to {14,} flips Quill7Basin from accepted to rejected, so raising the floor by four characters turns away an 11-character password that follows every other rule. The third row shows an edge case I'd not trust the tool alone on. "Quill Basin 7 Tower" is a perfectly good 19-character passphrase, yet my no-spaces lookahead rejects it, so the regex punishes exactly the long, memorable style that NIST guidance (minimum 8 characters, no composition rules required) encourages.

My call: keep the floor at 12 as a compromise, drop the \S+$ assertion so passphrases pass, and add a deny list for common values. The number to watch is the length bound, because it moves acceptance far more than any character-class rule. I cross-check by recomputing the two edge strings by hand, counting 11 and 19 characters, and both agree with the tester. Before release I also confirm that the tester verdict for each accepted string matches the server's verdict for the same string, since a mismatch between the two copies of the pattern would show up there first.

Regex Pattern Flavors for JavaScript Password Checks

Pick the regex flavor that matches your runtime. JavaScript uses ECMAScript rules in browsers and Node, so the pattern above works unchanged in RegExp.test(). Most patterns also port to PCRE in PHP, though lookbehind and Unicode classes can differ in older engines. Choose your flags deliberately: i would let lowercase letters satisfy the capital rule in some designs, so leave it off, and do not add g to a validation pattern.

When a rule never fires, the culprit is usually an unescaped character: in the pattern above the trailing hyphen in the symbol class is literal, and an unescaped dot or bracket added to it would silently stop that rule firing. Good pattern matching habits are to escape the hyphen in a class, test every branch, and keep the password regex validation logic in one shared function so the browser and server cannot disagree. A password validate call that returns a plain boolean keeps your error handling simple.

Limits of Password Complexity Checks with Regex

A regex checks shape, not safety. "Password123!" satisfies every rule above while sitting on every list of commonly used passwords, so pair the pattern with a deny list. Complexity rules also push people toward predictable symbol swaps, which is why the password regular expression should stay a simple gate rather than your whole policy. Aim for long, unique values and offer a manager, and treat a good regex pattern validator as one tool among several.

Frequently Asked Questions

Why use lookaheads instead of a simpler pattern?
A single linear pattern can't independently require multiple character classes to each appear somewhere in the string while also allowing them in any order -- lookaheads ((?=.*[A-Z]) etc.) let you say 'somewhere in this string there's an uppercase letter' as an independent condition, then the final .{min,max} enforces overall length. This is the standard technique for composable password regex requirements.
Is regex validation enough for password strength?
No -- a regex only checks composition rules (length, character classes), which NIST SP 800-63B guidance now considers a weaker signal than genuine unpredictability. A password can satisfy every regex requirement (P@ssw0rd1) and still be trivially guessable. Pair this with the Password Strength Checker or Pattern Analyzer for a real entropy-based assessment.
Will this regex work the same in JavaScript, Python, and PHP?
The core pattern is portable across all three, since lookaheads and character classes are standard regex features -- but the surrounding syntax differs (JavaScript wraps it in slashes, Python uses re.match(), PHP uses preg_match() with delimiters). The generated JavaScript example shows the pattern in context; adapt the wrapper syntax for your language.
Should I set a maximum length?
Only if your storage or downstream system genuinely requires one -- NIST guidance recommends allowing at least 64 characters to support passphrases, and capping too aggressively can block legitimately strong long passwords. Leave it blank unless you have a specific constraint.
My symbols aren't being escaped -- will that break the regex?
This tool automatically escapes regex-special characters within your symbol list (like ], \, ^, and -) so they're treated literally inside the character class rather than as regex syntax. You can safely type any symbols you want to allow.

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the password generators, strength checkers, hash tools and security calculators on PasswordHive.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Each tool's output is checked against official test vectors or a reference implementation, and every strength and crack-time figure against the formula it uses, before it goes live.
  • Published Updated