MSc(CS) FSD-1 Practical-2 Employee Registration details
Create an HTML form that contain the Employee Registration details and write a JavaScript to validate DOB, Joining Date, and Salary.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Registration Form</title>
<script>
// ES6 function to validate form fields
const validateForm = () => {
let isValid = true;
// Get input values
const dob = document.getElementById('dob').value;
const joiningDate = document.getElementById('joiningDate').value;
const salary = document.getElementById('salary').value;
// Get error elements
const dobError = document.getElementById('dobError');
const joiningDateError = document.getElementById('joiningDateError');
const salaryError = document.getElementById('salaryError');
// Reset errors
dobError.textContent = '';
joiningDateError.textContent = '';
salaryError.textContent = '';
// Validate DOB (should not be in the future)
const dobDate = new Date(dob);
const currentDate = new Date();
if (dobDate > currentDate) {
dobError.textContent = 'DOB cannot be in the future.';
isValid = false;
}
// Validate Joining Date (should not be in the future)
const joiningDateObj = new Date(joiningDate);
if (joiningDateObj > currentDate) {
joiningDateError.textContent = 'Joining date cannot be in the future.';
isValid = false;
}
// Validate Salary (should be a positive number)
if (!salary || salary <= 0) {
salaryError.textContent = 'Salary must be a positive number.';
isValid = false;
}
// If all validations pass, show success message
if (isValid) {
alert('Employee Registration Successful!');
document.getElementById('employeeForm').reset(); // Clear the form
}
};
</script>
</head>
<body>
<h2>Employee Registration</h2>
<form id="employeeForm">
<label for="dob">Date of Birth:</label>
<input type="date" id="dob">
<span id="dobError"></span>
<br><br>
<label for="joiningDate">Joining Date:</label>
<input type="date" id="joiningDate">
<span id="joiningDateError"></span>
<br><br>
<label for="salary">Salary:</label>
<input type="number" id="salary" placeholder="Enter your salary">
<span id="salaryError"></span>
<br><br>
<button type="button" onclick="validateForm()">Register</button>
</form>
</body>
</html>
Comments
Post a Comment