Last active
February 18, 2022 19:15
-
-
Save joshxduncan/a5f11cc2184c9cbd7edccd1dc6e55ce2 to your computer and use it in GitHub Desktop.
Revisions
-
joshxduncan revised this gist
Feb 18, 2022 . 1 changed file with 53 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,39 +2,76 @@ const name = document.getElementById('userName'); const email = document.getElementById('userEmail'); const form = document.getElementById('form'); const modal = document.querySelector('.modal'); const overlay = document.querySelector('.overlay'); const openBtn = document.querySelector('.openBtn'); const closeBtn = document.querySelector('.closeBtn'); /** * Update name and email of modal * @param name * @param email */ function updateModal(name, email) { const modalNameField = document.querySelector('.modal-user-name'); const modalEmailField = document.querySelector('.modal-user-email'); modalNameField.innerText = name; modalEmailField.innerText = email; } /** * This function validates a form * @param formElements the form object * @returns {boolean} */ function validateForm(form) { const formFields = form.elements; const userName = formFields["userName"].value; const userEmail = formFields["userEmail"].value; let valid = true; let errors = ""; // Validate the userName input field if (!userName.length > 2) { alert("Username is too short"); valid = false; } // Validate the userEmail input field if (!(userEmail.length > 7) || !(userEmail.includes("@", "."))) { alert("Invalid email entered"); valid = false; }; // Validate modal // @todo: Add modal validation if (valid) { updateModal(userName, userEmail); } return valid; } // Validate our form when the user submits it form.addEventListener('submit', (e) => { // Stop default browser submit actions e.preventDefault(); // Now we need to validate the form formValid = validateForm(form); // We can show the modal if the form was validated if (formValid === true) { modal.classList.add("active"); overlay.classList.add("active"); } }); // Close modal button functionality closeBtn.addEventListener('click', function(e) { e.preventDefault(); modal.classList.remove("active"); overlay.classList.remove("active"); }); -
joshxduncan created this gist
Feb 18, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ const name = document.getElementById('userName'); const email = document.getElementById('userEmail'); const form = document.getElementById('form'); const modal = document.getElementById('modal'); const overlay = document.querySelector('#overlay'); const openBtn = document.querySelector('.openBtn'); const closeBtn = document.querySelector('.closeBtn'); // We can hide the overlay/modal here, but that should be done in CSS // This is just an example overlay.style.display = "none"; modal.style.display = "none"; /** * This function validates a form * @param formElements the form object * @returns {boolean} */ function validateForm(form) { // Validate the userName input field const userName = form.elements["userName"].value; if (userName === "Camila") { alert("This girl should have been deported back to Argentina in 2018!"); return false; } else { alert("That name seems ok!") } } // Validate our form when the user submits it form.addEventListener('submit', (e) => { e.preventDefault(); // Just for testing, it will show the submit action is working console.log('submitted'); // Now we need to validate the form validateForm(form); });