Files
web/apps/scandic-web/utils/zod/phoneValidator.ts
2025-06-03 11:54:05 +00:00

22 lines
585 B
TypeScript

import { z } from "zod"
export function phoneValidator(
msg = "Required field",
invalidMsg = "Invalid type"
) {
return z
.string({ invalid_type_error: invalidMsg, required_error: msg })
.min(5, { message: "The number you have entered is too short" })
.superRefine((value, ctx) => {
if (value) {
const containsAlphabeticChars = /[a-z]/gi.test(value)
if (containsAlphabeticChars) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Please enter a valid phone number",
})
}
}
})
}