JS password validation code generator
Generate pasteable validation code from strength requirements: `const re = /…/;` (stacked (?=…) lookaheads for length and upper/lower/digit/symbol counts, optionally rejecting whitespace and repeated characters) plus a RULES array and a validatePassword() function returning { valid, failed }, with positive and negative test cases. Boundary: this is format validation only — no entropy scoring and no breached-password lookup.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
Result
The result can be copied directly.
Enter a sample password to generate the validation code
Characters: 0 · Lines: 0 · Rules: 0 · Minimum length: 0
What this tool does
- Generate password rules and messages for a signup form.
- Turn requirements like "8+ chars with upper, lower and digit" into testable code.
- Compare regex shapes and test cases for different rules.
Example
Input
Abc12345
Output
// 由 UniKit「JS 密码校验代码生成」生成的校验规则
// 长度 8–64,禁止空白字符
const re = /^(?=(?:.*[A-Z]){1})(?=(?:.*[a-z]){1})(?=(?:.*[0-9]){1})[^\s]{8,64}$/;
const RULES = [
{ key: 'length', test: value => value.length >= 8 && value.length <= 64, message: '长度需为 8-64 个字符' },
{ key: 'upper', test: value => (value.match(/[A-Z]/g) ?? []).length >= 1, message: '至少包含 1 个大写字母' },
{ key: 'lower', test: value => (value.match(/[a-z]/g) ?? []).length >= 1, message: '至少包含 1 个小写字母' },
{ key: 'digit', test: value => (value.match(/[0-9]/g) ?? []).length >= 1, message: '至少包含 1 个数字' },
{ key: 'noSpace', test: value => !/\s/.test(value), message: '不能包含空格等空白字符' },
];
/**
* 校验密码;返回 { valid, failed } —— failed 里是未通过的规则 key。
* @param {string} value 待校验的密码
*/
function validatePassword(value) {
if (typeof value !== 'string') return { valid: false, failed: ['type'] };
const failed = RULES.filter(rule => !rule.test(value)).map(rule => rule.key);
return { valid: failed.length === 0 && re.test(value), failed };
}
// 测试用例(可直接粘进 vitest / node 断言)
const CASES = [
{ value: "Abc12345", expect: validatePassword("Abc12345").valid },
{ value: 'a'.repeat(7), expect: false },
{ value: 'AAAAAAAA', expect: false },
];The generated regex checks each requirement with lookaheads and returns the failed rule keys, so a form can show a specific message.
Frequently asked questions
Why does the regex contain several (?=…)?
Each (?=(?:.*X){n}) is a lookahead requiring at least n occurrences of X (upper, lower, digit, symbol) without consuming characters, so they stack in one match; [^\s]{min,max} then constrains length and whitespace.
How are Chinese or emoji passwords counted?
Length uses the JS string length (emoji may count as 2). Upper/lower/digit rules only match ASCII, so an all-Chinese password fails for missing upper, lower and digit.
What does validatePassword return?
It returns `{ valid, failed }`: valid means every rule passed, and failed lists the keys that did not (length / upper / lower / digit / symbol / noSpace / noRepeat) so you can show a precise message.
Is this a strength estimate?
No. It only checks format rules — no entropy calculation and no breached-password lookup. Combine it with server-side policy and a password manager for real strength.
Keywords:password validation