I want to validate a phone number. If it has a value then it should output "Invalid mobile number format". If it is blank, then it should output "Mobile number is required".
Currently, it always outputs as "Invalid mobile number format".
I'm using Zod, react-hook-form and libphonenumber-js
import { isValidPhoneNumber } from "libphonenumber-js";
...
const schema = zod.object({
firstName: zod
.string({
required_error: "First name is required.",
})
.nonempty("First name is required."),
lastName: zod
.string({
required_error: "Last name is required.",
})
.nonempty("Last name is required."),
contactNumber: zod
.string({
required_error: "Mobile number is required.",
})
.transform((value) => `+${value}`)
.refine(isValidPhoneNumber, "Invalid mobile number format.")
.transform((value) => value.slice(1)),
});
...
<Controller
control={control}
name="contactNumber"
rules={{ required: true }}
render={({ field: { ref, ...field }, ...props }) => (
<StyledPhoneInput
enableSearch
enableTerritories
specialLabel="Mobile number "
inputProps={{
ref,
required: true,
placeholder: "+84",
}}
country={"vn"}
autoFormat
field={field}
muiProps={{ required: true, fullWidth: true }}
{...props}
/>
)}
/>
I don't exactly know what constitutes a minimum valid phone number, but basically it seems you need to validate some minimum input on the
contactNumberfield. From what I can tell, specifyingrequired_errorprop only seems to require a defined value.For example, if you had just
firstName: zod.string({ required_error: "First name is required." })then the value""is accepted whileundefinedthrows the error that"First name is required.". The.nonemptyvalidator appears to coerce the string into a character array and assert that it is not empty.You can assert the the
contactNumberalso has some minimal, non-zero, amount of input.Example: