forked from jaredpalmer/formik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncValidation.js
46 lines (43 loc) · 1.16 KB
/
SyncValidation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react';
import { Formik, Field, Form, ErrorMessage } from 'formik';
import { Debug } from './Debug';
const validate = values => {
const errors = {};
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 SignIn = () => (
<div>
<h1>Sign In</h1>
<Formik
initialValues={{
email: '',
password: '',
}}
validate={validate}
onSubmit={values => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2));
}, 500);
}}
render={({ errors, touched }) => (
<Form>
<label htmlFor="email">Email</label>
<Field name="email" placeholder="[email protected]" type="email" />
<div className="field-error">
<ErrorMessage name="email" />
</div>
<label htmlFor="password">Password</label>
<Field name="password" type="password" />
<button type="submit">Sign In</button>
<Debug />
</Form>
)}
/>
</div>
);
export default SignIn;