forked from Anjaliavv51/Retro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Anjaliavv51#107 from Daveless/add-validation-to-forms
forms validation Added
- Loading branch information
Showing
4 changed files
with
312 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,107 @@ | ||
import { useState } from "react"; | ||
|
||
const Form = () => { | ||
return ( | ||
<div> | ||
<form action=""> | ||
<div className="flex flex-col gap-4 md:px-4"> | ||
<div className="flex justify-between gap-2"> | ||
<input type="text" name="fname" id="" placeholder="First Name" className="placeholder-text" /> | ||
<input type="text" name="lname" placeholder="Last Name" className="placeholder-text" /> | ||
</div> | ||
<input type="email" placeholder="Write yout Email here" className="placeholder-text" /> | ||
<textarea name="message" id="" cols="30" rows="10" className="placeholder-text" placeholder="Your Massege"></textarea> | ||
<button className="bg-primary-green rounded-2xl w-[100px] p-3 font-semibold text-text-white" type="submit">Submit</button> | ||
</div> | ||
</form> | ||
const [contactForm, setContactForm] = useState({ | ||
firstName: "", | ||
lastName: "", | ||
email: "", | ||
message: "", | ||
}); | ||
|
||
const [contactErrors, setContactErrors] = useState({}); | ||
|
||
const handlechange = (e) => { | ||
setContactForm({ ...contactForm, [e.target.name]: e.target.value }); | ||
}; | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
const validationContactErrors = {}; | ||
if (!contactForm.firstName.trim()) { | ||
validationContactErrors.firstName = "First name is required"; | ||
} | ||
if (!contactForm.lastName.trim()) { | ||
validationContactErrors.lastName = "last name is required"; | ||
} | ||
if (!contactForm.email.trim()) { | ||
validationContactErrors.email = "email is required"; | ||
} | ||
if (!/\S+@\S+\.\S+/.test(contactForm.email)) { | ||
validationContactErrors.email = "email is not valid"; | ||
} | ||
if (!contactForm.message.trim()) { | ||
validationContactErrors.message = "message is required"; | ||
} | ||
setContactErrors(validationContactErrors); | ||
|
||
if (Object.keys(validationContactErrors).length === 0) { | ||
alert("Message sent successfully"); | ||
setContactForm({ | ||
firstName: "", | ||
lastName: "", | ||
email: "", | ||
message: "", | ||
}); | ||
} | ||
}; | ||
return ( | ||
<div> | ||
<form action="" onSubmit={handleSubmit}> | ||
<div className="flex flex-col gap-4 md:px-4"> | ||
<div className="flex justify-between gap-2"> | ||
<input | ||
type="text" | ||
name="firstName" | ||
id="" | ||
placeholder="First Name" | ||
onChange={handlechange} | ||
className="placeholder-text" | ||
/> | ||
<span className="pl-4 text-[#ff0000] text-sm"> | ||
{contactErrors.firstName} | ||
</span> | ||
<input | ||
type="text" | ||
name="lastName" | ||
placeholder="Last Name" | ||
onChange={handlechange} | ||
className="placeholder-text" | ||
/> | ||
<span className="pl-4 text-[#ff0000] text-sm"> | ||
{contactErrors.lastName} | ||
</span> | ||
</div> | ||
<input | ||
type="email" | ||
name="email" | ||
placeholder="Write yout Email here" | ||
onChange={handlechange} | ||
className="placeholder-text" | ||
/> | ||
<span className="pl-4 text-[#ff0000] text-sm"> | ||
{contactErrors.email} | ||
</span> | ||
<textarea | ||
name="message" | ||
cols="30" | ||
rows="10" | ||
onChange={handlechange} | ||
className="placeholder-text" | ||
placeholder="Your Massege" | ||
></textarea> | ||
<span className="pl-4 text-[#ff0000] text-sm"> | ||
{contactErrors.message} | ||
</span> | ||
<button | ||
className="bg-primary-green rounded-2xl w-[100px] p-3 font-semibold text-text-white" | ||
type="submit" | ||
> | ||
Submit | ||
</button> | ||
</div> | ||
) | ||
} | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Form; | ||
export default Form; |
This file contains 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
Oops, something went wrong.