Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Created January 8, 2026 07:02
Show Gist options
  • Select an option

  • Save Gaurav8757/0568c87bea969e100e379fbb40aecc61 to your computer and use it in GitHub Desktop.

Select an option

Save Gaurav8757/0568c87bea969e100e379fbb40aecc61 to your computer and use it in GitHub Desktop.
Regex Validation (Strong Patterns)
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