MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / Form

Function Form

examples/V7/basicValidation.tsx:7–95  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import './styles.css';
6
7export default function Form() {
8 const { register, errors, handleSubmit } = useForm();
9 const onSubmit = (data) => {
10 alert(JSON.stringify(data));
11 };
12 console.log(errors);
13
14 return (
15 <form onSubmit={handleSubmit(onSubmit)}>
16 <div>
17 <label htmlFor="firstName">First name</label>
18 <input
19 id="firstName"
20 type="text"
21 {...register('firstName', { required: true, maxLength: 80 })}
22 />
23 </div>
24 <div>
25 <label htmlFor="lastName">Last name</label>
26 <input
27 id="lastName"
28 type="text"
29 {...register('lastName', { required: true, maxLength: 100 })}
30 />
31 </div>
32 <div>
33 <label htmlFor="email">Email</label>
34 <input
35 id="email"
36 type="text"
37 {...register('email', {
38 required: true,
39 pattern:
40 /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
41 })}
42 />
43 </div>
44 <div>
45 <label htmlFor="mobileNumber">Mobile number</label>
46 <input
47 id="mobileNumber"
48 type="tel"
49 {...register('mobileNumber', {
50 required: true,
51 maxLength: 11,
52 minLength: 8,
53 })}
54 />
55 </div>
56 <div>
57 <label htmlFor="title">Title</label>
58 <select id="title" {...register('title', { required: true })}>
59 <option value="Mr">Mr</option>
60 <option value="Mrs">Mrs</option>
61 <option value="Miss">Miss</option>
62 <option value="Dr">Dr</option>
63 </select>
64 </div>

Callers

nothing calls this directly

Calls 3

useFormFunction · 0.85
handleSubmitFunction · 0.85
registerFunction · 0.85

Tested by

no test coverage detected