Created
January 8, 2026 07:02
-
-
Save Gaurav8757/0568c87bea969e100e379fbb40aecc61 to your computer and use it in GitHub Desktop.
Regex Validation (Strong Patterns)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const regexValidators = { | |
| fullName: z | |
| .string() | |
| .regex(/^[a-zA-Z0-9_]{3,16}$/, "Only letters, numbers & underscore"), | |
| phone: z | |
| .string() | |
| .regex(/^\+91\d{10}$/, "Phone must be +91XXXXXXXXXX"), | |
| strongPassword: z | |
| .string() | |
| .regex( | |
| /^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$/, | |
| "Password must contain 1 uppercase, 1 number, 1 symbol, 8+ chars" | |
| ), | |
| }; | |
| import { z } from "zod"; | |
| import { regexValidators } from "../services/regexValidators"; | |
| export const UserSchema = z.object({ | |
| fullName: regexValidators.fullName, | |
| email: z | |
| .string() | |
| .email("Invalid email") | |
| .refine( | |
| async (val) => { | |
| const exists = await checkEmailExists(val); | |
| return !exists; // return false for error | |
| }, | |
| { | |
| message: "Email already exists", | |
| } | |
| ), | |
| phone: regexValidators.phone, | |
| password: regexValidators.strongPassword, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment