Created
February 26, 2023 14:52
-
-
Save dcortesnet/5f4f4c8c5b21a7ce43052fe4bd88d02d to your computer and use it in GitHub Desktop.
React formik validate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment