22 lines
585 B
TypeScript
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",
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|