Skip to content

Instantly share code, notes, and snippets.

@cdpadilla42
Last active July 17, 2020 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdpadilla42/8b1407363644af63ca272d1146bbd544 to your computer and use it in GitHub Desktop.
Save cdpadilla42/8b1407363644af63ca272d1146bbd544 to your computer and use it in GitHub Desktop.
surefoot Code Challenges
<!-- FORM VALIDATION: Adds form validation for Name and Message Field -->
<script>
const signUpForm = document.querySelector('form.wpcf7-form');
function validateFormInputs(e) {
const errors = [];
const yourName = e.currentTarget['your-name'];
const yourMessage = e.currentTarget['your-message'];
console.log(yourName, yourMessage);
if (!yourName.value) {
errors.push(yourName);
}
if (!yourMessage.value) {
errors.push(yourMessage);
}
if (errors.length !== 0) {
e.preventDefault();
displayErrors(errors);
}
}
function displayErrors(errors) {
errors.forEach((input) => {
const alertElm = document.createElement('span');
alertElm.classList.add('wpcf7-not-valid-tip');
alertElm.setAttribute('role', 'alert');
alertElm.setAttribute('aria-hidden', 'true');
alertElm.textContent = 'The field is required.';
input.parentNode.appendChild(alertElm);
});
}
signUpForm.addEventListener('submit', validateFormInputs);
</script>
<!-- END FORM VALIDATION -->

Form Validation

Adding client side validation to surefoot's engineer contact form.

The attached script is used to add further validation to the form. The validation keeps responses quick by working in the browser before sending any data to the servers.

The code listens for the user to hit submit on the form. Once done, the script will check the message and name fields to see if there is any text written. If not, the code will add a red error message directly below the erroneous field. Otherwise, the form submits successfully.

This function can be added by copying and pasting the entire code snippet into the html document, one line directly above the tag.

Swap Contact Position

Switching the position of call prompt and contact form submission on engineering contact page

The attached script is used to switch the position of the two prompting elements on the contact form - the message form and the call prompt.

The script finds the section of code holding these two components. The code uses a unique id to target the exact code that is wrapping around them. In Between the unique wrapping it finds and the components that need switching are other wrappers. To make the switch, the script drills down from the unique wrapper to find the actual closest wrapper to those two components. Once it has found that nearest wrapper, from there the code is able to remove the email form component from the document and move it to the other side of the call component.

This function can be added by copying and pasting the entire code snippet into the html document, one line directly above the tag.

<!-- SWAP ELEMENTS: Switches the form component with the call prompt -->
<script>
const contentWrapper = document.querySelector('section#content');
const closestWrapper = contentWrapper.lastElementChild.querySelector('.container').firstElementChild
const emailFormElm = closestWrapper.firstElementChild;
closestWrapper.appendChild(emailFormElm);
</script>
<!-- END SWAP ELEMENTS -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment