Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
formik validation on phone number not working

in the phone number text field if we focus and defocus that time error should be happen, but its not happening when we focus and de focus the text field. When user give focus to the test field that time nothing is reflecting. Its also check the validation of phone number

import React, { useState } from "react";
import { useFormik } from "formik";
import * as Yup from "yup";
export default function DropDown() {
  const [phNoindex, setIndex] = React.useState(0);
  const [fields, setFields] = React.useState([{ value: "" }]);
  const formik = useFormik({
    initialValues: {
      phoneNumbers: {
        primaryPhone: "",
        secondaryPhone: "",
      },
    },
    validationSchema: Yup.object({
      phoneNumbers: Yup.object().shape({
        primaryPhone: Yup.string()
          // .matches(/^[^a-zA-Z]{8,15}$/, "")
          .required("Phone number is required").min(10,"Phone number is required"),
      }),
    }),  
    onSubmit: (values) => {
      const data = {
        phoneNumbers: {
          primaryPhone: values.phoneNumbers.primaryPhone,
          secondaryPhone: values.phoneNumbers.secondaryPhone,
        },
      };
    },
  });
  function handleChange(i, event) {
    if (event) {
      const values = [...fields];
      values[i].value = event;
      setFields((v) => values);
      formik.setFieldValue("phoneNumbers", {
        primaryPhone: values[0]?.value,
        secondaryPhone: values[1]?.value,
      });
    }
  }
  return (
    <>
    <form onSubmit={formik.handleSubmit}>
    {fields.map((field, idx) => {
                      return (
                         <div
                            key={`${field}-${idx}`}
                            style={{ marginBottom: "18px" }}>
                            <input
                              id="phoneNumbers1"
                              type="text"
                              placeholder="Enter phone number "
                              helperText={formik.touched.phoneNumbers && formik.errors.phoneNumbers}
                              defaultCountry="IN"
                              international
                              name="phoneNumbers"
                              autoComplete="off"
                              onBlur={formik.handleBlur}
                              onChange={(e) => {handleChange(idx, e);}}
                              onClick={() => setIndex(idx)}
                            />
                            </div>)})}
         </form>
    </>
  );
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!