Skip to content

Commit

Permalink
Merge pull request #73 from Anuragpal010104/CartAlert
Browse files Browse the repository at this point in the history
Added form validation on signup page
  • Loading branch information
Karamraj authored Oct 3, 2023
2 parents 3d364e4 + 56d8d57 commit 7713486
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions SignUpModal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@
<div class="row d-flex align-items-center justify-content-center h-100">
<div class="col-md-8 col-lg-7 col-xl-6">
<img src="../IMG/deafults/sign_up.jpg" class="img-fluid vert-move" alt="Phone image">

</div>
<div class="col-md-7 col-lg-5 col-xl-5 offset-xl-1">
<h2 class="text-center text-danger">Sign Up</h2>
<form>
<div class="col-md-4">
<label for="validationCustom04" class="form-label">Email or Phone Number</label>
<input
type="text"
class="form-control"
id="validationCustom04"
required
/>
<div class="invalid-feedback">Please provide a valid email or phone number.</div>
</div>
<!-- First Name input -->
<div class="form-outline mb-3">
<label class="form-label" for="form1Example13">First Name</label>
Expand Down
20 changes: 19 additions & 1 deletion SignUpModal/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
form.addEventListener(
"submit",
(event) => {
if (!form.checkValidity()) {
const emailOrPhoneInput = form.querySelector("#validationCustom04");
const emailOrPhoneValue = emailOrPhoneInput.value.trim();
if (!form.checkValidity() || !isEmailOrPhoneUnique(emailOrPhoneValue)) {
event.preventDefault();
event.stopPropagation();
}
Expand All @@ -14,6 +16,20 @@
false
);
});
// Function to check if email or phone number is unique
function isEmailOrPhoneUnique(value) {
// Retrieve previously stored user data from localStorage
const storedUserData = JSON.parse(localStorage.getItem("userData"));

if (storedUserData && storedUserData.emailOrPhone === value) {
// Not unique - show error message
const emailOrPhoneInput = document.querySelector("#validationCustom04");
emailOrPhoneInput.classList.add("is-invalid");
return false;
}

return true;
}
})();

// onsubmit section
Expand All @@ -23,6 +39,8 @@ const form = document.querySelector(".needs-validation");

submitBtn.addEventListener("click", (event) => {
userData = {
emailOrPhone:
event.target.parentElement.parentElement.children[6].children[1].value,
firtsName:
event.target.parentElement.parentElement.children[0].children[1].value,
lastName:
Expand Down

0 comments on commit 7713486

Please sign in to comment.