Yup phone number validation doesn't works
You are validating it as a number and not as a string, thus .min
in this case means that number value has to be minimum 7. It's not a length. You have to validate it as a string and use a regexp pattern. Example:
phoneSchema = Yup.string().matches(new RegExp('[0-9]{7}'))
And then you can also make it more advanced.
Regular expression to validate US phone numbers using Formik and Yup
Try this regex it can meet your requirements
/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/
It would look something like that:
validationSchema={Yup.object({
AdministratorCell: Yup.string()
.matches(/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/, {
message: "Invalid phone number",
excludeEmptyString: false,
})
})}
How to validate material-ui-phone-number with Yup
You can always use Controller
component to integrate with any third-party input components that do not work out-of-the-box with react-hook-form
.
const { control, ... } = useForm();
...
<Controller
name="phone"
control={control}
render={({ name, onBlur, onChange, value }) => (
<MuiPhoneNumber
name={name}
value={value}
onBlur={onBlur}
// onChange pass the raw value so you can access it using e instead of
// e.target.value. props.onChange can accept the value directly
onChange={onChange}
{...}
/>
)}
/>
Live Demo
Related Topics
How to Get the Ajax Response from Success and Assign It in a Variable Using Jquery
Expected Linebreaks to Be 'Lf' But Found 'Crlf' Linebreak-Style
Prevent Form Submission on Enter Key Press
Typescript Property Does Not Exist on Type {}
Blob Download Is Not Working in Ie
How to Get the Id of a Button That Was Clicked - Reactjs
Howto Trigger Input Event Programmatically on Input Text Field
Connecting to Tcp Socket from Browser Using JavaScript
Replace Multiple Strings With Multiple Other Strings
How to Validate This Select Option If Not Selected
Stop Just One Dropdown Toggle from Closing on Click
Make Discord Bot Send Picture With Message With Nodejs
Javascript Sum and Group by of Json Data
How to Wait for Element to Disappear in Cypress
Angular [Innerhtml]: How to Render Additional Elements Inside an Element With [Innerhtml] Directive
How to Instantiate a JavaScript Class in Another Js File
Vuejs Set Active Class, When One Li Element Clicked in V-For Loop