Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

formik validator in react

import React from 'react';
import { useFormik } from 'formik';

// A custom validation function. This must return an object
// which keys are symmetrical to our values/initialValues
const validate = values => {
  const errors = {};
  if (!values.firstName) {
    errors.firstName = 'Required';
  } else if (values.firstName.length > 15) {
    errors.firstName = 'Must be 15 characters or less';
  }

  if (!values.lastName) {
    errors.lastName = 'Required';
  } else if (values.lastName.length > 20) {
    errors.lastName = 'Must be 20 characters or less';
  }

  if (!values.email) {
    errors.email = 'Required';
  } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i.test(values.email)) {
    errors.email = 'Invalid email address';
  }

  return errors;
};

const SignupForm = () => {
  // Pass the useFormik() hook initial form values, a validate function that will be called when
  // form values change or fields are blurred, and a submit function that will
  // be called when the form is submitted
  const formik = useFormik({
    initialValues: {
      firstName: '',
      lastName: '',
      email: '',
    },
    validate,
    onSubmit: values => {
      alert(JSON.stringify(values, null, 2));
    },
  });
  return (
    <form onSubmit={formik.handleSubmit}>
      <label htmlFor="firstName">First Name</label>
      <input
        id="firstName"
        name="firstName"
        type="text"
        onChange={formik.handleChange}
        value={formik.values.firstName}
      />
      {formik.errors.firstName ? <div>{formik.errors.firstName}</div> : null}

      <label htmlFor="lastName">Last Name</label>
      <input
        id="lastName"
        name="lastName"
        type="text"
        onChange={formik.handleChange}
        value={formik.values.lastName}
      />
      {formik.errors.lastName ? <div>{formik.errors.lastName}</div> : null}

      <label htmlFor="email">Email Address</label>
      <input
        id="email"
        name="email"
        type="email"
        onChange={formik.handleChange}
        value={formik.values.email}
      />
      {formik.errors.email ? <div>{formik.errors.email}</div> : null}

      <button type="submit">Submit</button>
    </form>
  );
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: find capital word in string javascript 
Javascript :: how to scroll to an element javascript react 
Javascript :: angular findindex object based array 
Javascript :: find all subsets of an array javascript 
Javascript :: img src in react js 
Javascript :: ignore eslint warning one line 
Javascript :: array sort by two properties 
Javascript :: electron check if file exists 
Javascript :: print all variables defined javascript 
Javascript :: float to euro curency 
Javascript :: check online status javascript 
Javascript :: jquery set hidden field value 
Javascript :: javascript array move element one position 
Javascript :: js how to reverse a string 
Javascript :: reset input react 
Javascript :: StatusBar 
Javascript :: push an property and value to an object in javascript 
Javascript :: send serialized form data jquery 
Javascript :: clear html element javascript 
Javascript :: Remove Duplicates array values in javascript 
Javascript :: datatable get row data 
Javascript :: react native navigation navigate 
Javascript :: How to use body-parser package in using npm 
Javascript :: javascript json append array 
Javascript :: promise.all async await 
Javascript :: javascript declare a variable 
Javascript :: javascript tick marks 
Javascript :: save canvas as image from website 
Javascript :: express get remote ip 
Javascript :: simple kick command discord.js v12 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =