Skip to content

Commit

Permalink
Fix form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wkktoria committed Jan 16, 2025
1 parent eb29652 commit 71363a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
22 changes: 18 additions & 4 deletions 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
} else {
document.body.classList.remove("dark-mode");
}
console.log(getCookie("IsDarkMode"));
</script>
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
Expand All @@ -54,11 +53,26 @@

document.getElementById('contact-form').addEventListener('submit', async function (event) {
event.preventDefault();
await emailjs.sendForm('service_4uw82xw', 'template_fy4oy5t', '#contact-form', this).then(() => {
document.getElementById('contact-form').reset();
});

if (isFormValid()) {
await emailjs.sendForm('service_4uw82xw', 'template_fy4oy5t', '#contact-form', this).then(() => {
document.getElementById('contact-form').reset();
});
}
});
}

function isFormValid() {
const form = document.getElementById('contact-form');
const name = form['user_name'].value;
const email = form['user_email'].value;
const message = form['message'].value;

const emailRegex = /^\S+@\S+\.\S+$/;
const isEmailValid = emailRegex.test(email);

return name.length >= 3 && isEmailValid && message.length >= 10;
}
</script>
</body>
</html>
22 changes: 18 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
} else {
document.body.classList.remove("dark-mode");
}
console.log(getCookie("IsDarkMode"));
</script>
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
Expand All @@ -54,11 +53,26 @@

document.getElementById('contact-form').addEventListener('submit', async function (event) {
event.preventDefault();
await emailjs.sendForm('service_4uw82xw', 'template_fy4oy5t', '#contact-form', this).then(() => {
document.getElementById('contact-form').reset();
});

if (isFormValid()) {
await emailjs.sendForm('service_4uw82xw', 'template_fy4oy5t', '#contact-form', this).then(() => {
document.getElementById('contact-form').reset();
});
}
});
}

function isFormValid() {
const form = document.getElementById('contact-form');
const name = form['user_name'].value;
const email = form['user_email'].value;
const message = form['message'].value;

const emailRegex = /^\S+@\S+\.\S+$/;
const isEmailValid = emailRegex.test(email);

return name.length >= 3 && isEmailValid && message.length >= 10;
}
</script>
</body>
</html>

0 comments on commit 71363a9

Please sign in to comment.