Frontend Mentor - Newsletter sign-up form with success message solution

This is a solution to the Newsletter sign-up form with success message challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.

Table of contents

Overview

The challenge

Users should be able to:

  • Add their email and submit the form
  • See a success message with their email after successfully submitting the form
  • See form validation messages if:
    • The field is left empty
    • The email address is not formatted correctly
  • View the optimal layout for the interface depending on their device's screen size
  • See hover and focus states for all interactive elements on the page

Screenshot

desktop-normal desktop-view-email-alert desktop-email-validation desktop-active-btn success-view success-view-active mobile-active-states mobile-success-view

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox
  • CSS Grid
  • Mobile-first workflow

What I learned

  <form  id="form" action="#">
    <div id="input-control">
      <label for="email">Email Address</label>
      <span id="error-msg"></span>
      <input type="email"
         id="email"
         name="email"
         placeholder="email@company.com"
         required>
      <button class="button submit-btn" type="submit">Subscribe to Monthly Newsletter</button>
    </div>
  </form>
  :root {
    --dark-slate-grey: hsl(234, 29%, 20%);
    --charcoal-grey: hsl(235, 18%, 26%);
    --grey: hsl(231, 7%, 60%);
    --white: hsl(0, 0%, 100%);
    --tomato: hsl(4, 100%, 67%);
  }
  submitBtn.addEventListener('click', () => {
    const emailInput = document.querySelector('#email');
    const userEmail = emailInput.value;
    if(userEmail === '') {
      alert("Please enter an email address!")
      emailInput.style.border = "solid red 2px";
    } else {
      console.log(userEmail);
      successCard.style.visibility = 'visible';
      previewCard.remove();
      document.querySelector(".user-email").innerText = userEmail;
    }
  })

Useful resources

Author