Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
2.5 kB
3
Indexable
Never
document.addEventListener("DOMContentLoaded", function () {
  const registrationForm = document.getElementById("registrationForm");

  registrationForm.addEventListener("submit", function (event) {
    event.preventDefault();

    const firstName = document.getElementById("firstName").value;
    const lastName = document.getElementById("lastName").value;
    const dob = new Date(document.getElementById("dob").value);
    const email = document.getElementById("email").value;
    const address = document.getElementById("address").value;
    const contactNumber = document.getElementById("contactNumber").value;

    var errorMessage = document.getElementById("errorMessage");
    errorMessage.innerText = "";

    if (dob <= new Date("1924-01-01")) {
      errorMessage.innerText = "Choose a date greater than 1/1/1924";
      return false;
    }

    if (!/^\d{10}$/.test(contactNumber)) {
      errorMessage.innerText = "Enter a valid contact number";
      return false;
    }

    if (!isValidEmail(email)) {
      errorMessage.innerText = "Please enter a valid mail id";
      return false;
    }

    const passengerId = generatePassengerId();
    const password = generatePassword(firstName);

    const passengerDetails = {
      passengerId: passengerId,
      password: password,
      firstName: firstName,
      lastName: lastName,
      dob: dob,
      email: email,
      address: address,
      contactNumber: contactNumber,
    };

    localStorage.setItem("passengerDetails", JSON.stringify(passengerDetails));
    displayAcknowledgment(passengerId, password);
  });

  function isValidEmail(email) {
    const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return regex.test(email);
  }

  function generatePassengerId() {
    const passengerId = Math.floor(10000 + Math.random() * 90000);
    return passengerId;
  }

  function generatePassword(firstName) {
    return firstName.slice(0, 4).toLowerCase() + "@123";
  }

  function displayAcknowledgment(passengerId, password) {
    const message = `Passenger Registration is successful.\n\nPassenger ID: ${passengerId}\nPassword: ${password}`;
    alert(message);
  }

  const resetButton = document.querySelector('input[type="reset"]');
  resetButton.addEventListener("click", function (event) {
    event.preventDefault();
    const confirmation = confirm("Is it okay to reset the fields?");
    if (confirmation) {
      registrationForm.reset();
    }
  });
});
Leave a Comment