Coding
AI Prompt for Validation Regex
Build validation regex for passwords, emails and phones with this AI prompt: per-rule patterns, user-friendly feedback, and common pitfalls avoided.
The prompt
Role: You are a regex expert who builds validation patterns that are strict but fair — they reject bad input without rejecting valid users. Context: Regex flavor: [FLAVOR]. Validating: [TYPE: password / email / phone / username / etc.]. Rules: [RULES, e.g. min 12 chars, 1 uppercase, 1 digit, 1 symbol, no spaces] UX requirement: [UX, e.g. show which rules pass live, single error message]. Task: 1) Build the validation regex for [FLAVOR]. 2) Break it into per-rule pieces and explain each. 3) List valid inputs it accepts AND legitimate-looking inputs it rejects (with reasons) — the goal is zero false rejections of real users. 4) If [UX] needs per-rule feedback, provide the individual rule checks as separate patterns/functions instead of one mega-regex. 5) Warn about common pitfalls for [TYPE] validation (e.g. over-strict email regexes rejecting valid addresses). Format: Pattern first, then the rule breakdown, then accept/reject examples, then the UX implementation, then pitfalls. Constraints: Favor accepting valid users over rejecting edge-case abusers — note where you made that trade-off. For emails, do not try to fully implement RFC 5322; validate pragmatically.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[FLAVOR] [TYPE] [RULES] [UX]
Example input
FLAVOR=JavaScript, TYPE=password, RULES=min 12 chars, 1 uppercase, 1 number, 1 symbol, no spaces, UX=show which rules pass as user types
How to use this prompt
List the exact rules — password/email/phone requirements vary by product. Ask for the per-rule breakdown so users get helpful feedback, not just 'invalid'.
Tips for the best results
- For live UX feedback, always take the per-rule version over the single mega-regex.
- Ask what real-world valid inputs the pattern rejects — false rejections lose users.
- Pair the regex with a confirmation step (verification email/SMS) for critical fields.
Frequently asked questions
Are complex password regexes good UX?
It will warn you about the trade-off: strict regexes frustrate users, so it also suggests the UX-friendly checklist approach.
Why not use one giant regex for all password rules?
Users need to know WHICH rule failed. Per-rule checks give live feedback; a mega-regex only says invalid. This prompt generates both so to choose.