-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformformik.js
75 lines (70 loc) · 2.47 KB
/
formformik.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Formik,Form,Field, ErrorMessage } from "formik";
const validateEmail = RegExp(
/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
);
function Formikform()
{
return(
<Formik
initialValues={
{
name:"",
emailId:""
}}
validate={values=>{
const errors={};
if (values.name.length <= 5) {
errors.name = "username should be more than 5 characters";
}
else if (values.emailId.length <= 5)
errors.emailId = "email should be more than 5 characters";
if (!validateEmail.test(values.emailId)) {
errors.emailId = "Invalid";
}
return errors;
}}
onSubmit={values=>{
console.log(values)
}}>
{/* {({values,handleSubmit,handleChange,errors,handleBlur}) */}
{() => {
return (
<>
<Form >
<div>
<label>Username </label>
<Field name="name" type="text"></Field>
{/* <input
type="text"
value={values.name}
onChange={handleChange}
name="name"
onBlur={handleBlur}
></input> */}
<ErrorMessage name="name"/>
{/* <span>{errors.name}</span> */}
{/* {touched.firstName && errors.name ? (<div>{errors.name}</div> ) : null} */}
</div><br/>
<div>
<label>Email </label>
<Field name="emailId" type="email"></Field>
{/* <input
type="email"
value={values.emailId}
onChange={handleChange}
onBlur={handleBlur}
name="emailId"
></input> */}
<ErrorMessage name="emailId"/>
{/* <span>{errors.emailId}</span> */}
{/* {touched.emailId && errors.emailId ? (<div>{errors.emailId}</div> ) : null} */}
</div><br/>
<button type="submit">Submit</button>
</Form>
</>
);
}}
</Formik>
)
}
export default Formikform