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

Popular posts from this blog

Slip 11: Write node js application that transfer a file as an attachment on web and enables browser to prompt the user to download file using express js.

Slip 7 Create a node js file named main.js for event-driven application. There should be a main loop that listens for events, and then triggers a callback function when one of those events is detected.

Slip 1(a) Write an Angular 13 application addition of two numbers using ng-init, ng-model & ng-bind. And also demonstrate ng-show, ng-disabled, ng-click directives on button component.