Created
April 12, 2020 07:40
-
-
Save haldarmahesh/63c2c60f41d09d75fefd1f283417285d to your computer and use it in GitHub Desktop.
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 { useForm } from "react-hook-form"; | |
function onSubmitForm(formData) { | |
console.log("The status of formData", formData); | |
alert("Hi your phone number is: " + formData.phoneNumber); | |
} | |
export default function MyForm() { | |
const { register, handleSubmit, errors, watch } = useForm(); | |
console.log("ERR", errors); | |
return ( | |
<> | |
<h1> | |
Your phone number is: | |
<br /> {watch("phoneNumber")} | |
</h1> | |
<form onSubmit={handleSubmit(onSubmitForm)}> | |
<label> | |
Phone Number: | |
<input | |
type="text" | |
name="phoneNumber" | |
ref={register({ | |
required: { | |
value: true, | |
message: "Phone number is required", | |
}, | |
minLength: { | |
value: 12, | |
message: "Phone number should be minimum length of 12", | |
}, | |
})} | |
/> | |
</label> | |
<input type="submit" value="Submit" /> | |
</form> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment