score: 100/100
directions
Create form data validation using JavaScript. You will use the HTML document Laboratory2.html and styles.css files from Lab 2.
instructions
-
Open and Save HTML File
- Open the Laboratory2.html file and save it as Laboratory6.html.
-
Create the Form
- Within the
<body>of the file, create a<form>element with the following attributes: action: Set to submit_form.php method: Set to post onsubmit: Set to validateForm(event)
- Within the
-
Edit HTML Form
- Remove all required attributes from the form elements to customize the display message.
-
Enhance the Form with JavaScript Validation
- Implement JavaScript to validate the form data before submission.
Ensure that:
All required fields are properly validated.
Appropriate error messages are shown for invalid inputs using the alert method for the following:
Ø First andLast Name: "Please enterboth your first and last name"
Ø Email: "Please enter your email"
Ø Date of Birth: "Please select your date of birth"
Ø Gender: "Please select your gender"
Ø Address: "Please enter your address"
Ø Country: "Please select your country"
Ø Terms: "You must agree to the terms and conditions"
- Confirmation Dialog
- After the user clicks the submit button, a confirmation dialog should appear asking: "Are you sure you want to submit?"
- Use the confirm method for the confirmation dialog. If the user clicks "OK", display a message saying, "Successfully Submitted". If the user clicks "Cancel", return to the previous page to allow further editing.
code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Laboratory 6</title>
</head>
<body>
<form action="submit_form.php" method="post" onsubmit="validateForm(event)">
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" placeholder="e.g., Juan">
</div>
<div class="form-group">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" placeholder="e.g., Dela Cruz">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="e.g., juandelacruz@example.com">
</div>
<div class="form-group">
<label for="dob">Date of Birth</label>
<input type="date" id="dob" name="dob">
</div>
<div class="form-group">
<label>Gender</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea name="address" id="address" rows="3" placeholder="e.g., Street Name, Barangay, City, Province"></textarea>
</div>
<div class="form-group">
<label for="country">Country</label>
<select id="country" name="country">
<option value="" disabled selected>Select your country</option>
<option value="PH">Philippines</option>
<option value="ID">Indonesia</option>
<option value="MY">Malaysia</option>
<option value="MM">Myanmar</option>
<option value="SG">Singapore</option>
<option value="TH">Thailand</option>
<option value="VN">Vietnam</option>
</select>
</div>
<input type="checkbox" id="terms" name="terms">
<label for="terms">Agree to the <a href="">Terms and Conditions</a></label>
<button type="submit">Sign Up</button>
</form>
<script>
function validateForm(event) {
event.preventDefault();
const fname = document.getElementById("fname").value;
const lname = document.getElementById("lname").value;
const email = document.getElementById("email").value;
const dob = document.getElementById("dob").value;
const gender = document.querySelector('input[name="gender"]:checked');
const address = document.getElementById("address").value;
const country = document.getElementById("country").value;
const terms = document.getElementById("terms").checked;
if (fname === "" || lname === "") {
alert('Please enter both your first and last name');
return false;
}
if (email === "") {
alert("Please enter your email");
return false;
}
if (dob === "") {
alert("Please select your date of birth");
return false;
}
if (!gender) {
alert("Please select your gender");
return false;
}
if (address === "") {
alert("Please enter your address");
return false;
}
if (country === "") {
alert("Please select your country");
return false;
}
if (!terms) {
alert("You must agree to the terms and conditions");
return false;
}
if (!confirm("Are you sure you want to submit?")) {
return false;
}
alert("Successfully Submitted");
return true;
}
</script>
</body>
</html>@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Poppins', sans-serif;
background-color: #fafafa;
color: #171717;
}
form {
margin: 5rem auto;
max-width: 330px;
padding: 1rem;
border-radius: 8px;
border: 2px solid #ebebeb;
background-color: #fff;
}
h2 {
text-align: center;
}
form .form-group:nth-child(1) {
margin: 0;
}
textarea {
resize: none;
width: 100%;
}
button {
font-weight: 500;
font-size: 0.875rem;
line-height: 1.25rem;
color: white;
margin-top: 1rem;
padding: 0.5rem 1rem;
width: 100%;
border-radius: 6px;
border: none;
background-color: #0072f5;
}
.form-group {
margin: 1rem 0;
}
input[type='text'],
input[type='email'],
input[type='date'] {
width: 100%;
}
label {
display: block;
margin-right: 0.5rem;
font-weight: 500;
font-size: 0.875rem;
line-height: 1.25rem;
}
a {
color: #52aeff;
text-decoration: underline;
}
a:active {
color: red;
}
label[for='terms'] {
display: inline;
font-size: 0.75rem;
line-height: 1rem;
width: 100%;
margin-left: 0.25rem;
user-select: none;
}
textarea::placeholder,
input::placeholder {
color: #cbcbcb;
}
textarea,
select,
input {
padding: 0.5rem;
background-color: #fafafa;
border: 1px solid #ebebeb;
border-radius: 6px;
color: #666;
}
textarea:hover,
select:hover,
input:hover {
background-color: #f7f7f7;
border: 1px solid #c9c9c9;
}
textarea:focus,
select:focus,
input:focus {
outline: #cce6ff solid 2px;
}
input[type='radio']:focus,
input[type='checkbox']:focus {
outline: none;
}
input[type='radio'],
label[for='male'],
label[for='female'] {
display: inline;
font-weight: 400;
color: #666;
}