Build a Newsletter Signup Form Website

abhishekgurjar

Abhishek Gurjar

Posted on August 30, 2024

Build a Newsletter Signup Form Website

Introduction

Hello, developers! I’m excited to share my latest project: a Newsletter Signup Form. This project is perfect for those looking to create a functional and visually appealing form that collects user email addresses for newsletters using HTML, CSS, and JavaScript. It’s a great way to enhance your frontend development skills and build a useful tool for managing subscriptions.

Project Overview

The Newsletter Signup Form is a web application designed to allow users to subscribe to newsletters. The form includes email validation and displays a success message upon successful subscription. With a clean and interactive design, this project demonstrates how to create a practical and user-friendly form.

Features

  • Email Validation: Ensures that users enter a valid email address before submission.
  • Success Message: Displays a confirmation message after a successful subscription.
  • Responsive Design: The form is fully responsive, ensuring it looks great on both desktop and mobile devices.

Technologies Used

  • HTML: Provides the structure for the Newsletter Signup Form.
  • CSS: Styles the form to make it visually appealing and user-friendly.
  • JavaScript: Handles email validation and displays the success message based on user input.

Project Structure

Here’s an overview of the project structure:

Newsletter-Signup-Form/
├── index.html
├── style.css
└── script.js
Enter fullscreen mode Exit fullscreen mode
  • index.html: Contains the HTML structure for the Newsletter Signup Form.
  • style.css: Includes CSS styles to create a modern and responsive design.
  • script.js: Manages the interactive elements and handles email validation and success messaging.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Newsletter-Signup-Form.git
    
  2. Open the project directory:

    cd Newsletter-Signup-Form
    
  3. Run the project:

    • Open the index.html file in a web browser to view the Newsletter Signup Form.

Usage

  1. Open the website in a web browser.
  2. Enter your email address into the input field.
  3. Click the Subscribe button to submit your email.
  4. View the success message displayed after successful subscription.

Code Explanation

HTML

The index.html file defines the structure of the Newsletter Signup Form, including input fields, buttons, and result display areas. Here’s a snippet:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Newsletter Signup Form</title>
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,regular,500,700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
  <script src="./script.js" defer></script>
</head>
<body>
  <div class="container">
    <div class="box">
      <div class="left-box">
        <h1>Stay Updated!</h1>
        <p>Join our mailing list to receive updates and promotions.</p>
        <div class="email-text">
          <p>Email Address</p>
          <p class="not-valid">Valid Email Required</p>
        </div>
        <form>
          <input type="email" class="email-input" placeholder="email@company.com">
          <input type="submit" class="button" value="Subscribe to Newsletter">
        </form>
      </div>
      <div class="right-box">
        <img src="./assets/images/illustration-sign-up-desktop.svg" alt="">
      </div>
    </div>
  </div>
  <div class="footer">
    <p>Made with ❤️ by Abhishek Gurjar</p>
  </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS

The style.css file styles the Newsletter Signup Form, making it attractive and easy to use. Below are some key styles:

* {
  box-sizing: border-box;
}

body {
  font-family: Roboto, sans-serif;
  margin: 0;
  padding: 0;
  background-color: #36384e;
}

.container {
  max-width: 1240px;
  margin: 0 auto;
}

.box {
  gap: 20px;
  max-width: 70%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 30px;
  margin-inline: auto;
  background-color: white;
  border-radius: 15px;
}

.left-box {
  margin: 20px;
  width: 50%;
}

.left-box h1 {
  font-size: 50px;
}

.left-box p {
  font-size: 20px;
}

.email-text {
  display: flex;
  align-items: center;
  justify-content: center;
}

.success {
  display: inline;
}

.success-icon {
  width: 27px;
}

.email-text {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.not-valid {
  color: red;
  display: none;
}

input {
  font-size: 20px;
  width: 100%;
  height: 50px;
  border-radius: 15px;
  border: 2px solid black;
}

.button {
  font-size: 20px;
  width: 100%;
  border-radius: 15px;
  background-color: #242742;
  color: white;
}

.button:hover {
  background-color: #ff644b;
  cursor: pointer;
}

.right-box {
  width: 50%;
  margin: 0 20px;
}

.right-box img {
  width: 100%;
}

.footer {
  color: white;
  margin: 30px;
  text-align: center;
}

@media (max-width: 1200px) {
  .box {
    flex-direction: column-reverse;
  }
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

The script.js file contains the logic for handling email validation and displaying the success message. Here’s a snippet:

const submitBtn = document.getElementsByClassName("button")[0];
const emailInput = document.getElementsByClassName("email-input")[0];
const error = document.getElementsByClassName("not-valid")[0];
const box = document.getElementsByClassName("box")[0];

submitBtn.addEventListener("click", (event) => {
  event.preventDefault();
  const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  const isValid = emailPattern.test(emailInput.value);

  if (!isValid) {
    error.style.display = "block";
  } else {
    error.style.display = "none"; // Hide the error message if email is valid
    box.style.display = "none";

    // Create and show the message
    const message = document.createElement("div");
    message.className = "message";
    message.innerHTML = `
      <div class="message-content">
        <img src="./assets/images/icon-success.svg" alt="">
        <h1>Thanks for subscribing!</h1>
        <p>
          A confirmation email has been sent to ${emailInput.value}. Please open
          it and click the button inside to confirm your subscription.
        </p>
        <h2 class="closeBtn">Dismiss message</h2>
      </div>`;

    // Append the message to the body
    document.body.appendChild(message);

    // Select the close button from the newly created message element
    const closeBtn = message.querySelector(".closeBtn");
    closeBtn.addEventListener("click", () => {
      message.remove();
      location.reload(); // Reload the website
    });
  }
});
Enter fullscreen mode Exit fullscreen mode

Live Demo

You can check out the live demo of the Newsletter Signup Form project here.

Conclusion

Creating the Newsletter Signup Form was an excellent way to apply frontend development skills to build a functional and engaging tool. This project demonstrates how to create an interactive and responsive form that can be used for managing email subscriptions. I hope it inspires you to build your own tools and enhance your web development skills. Happy coding!

Credits

This project was developed as part of my continuous learning journey in web development.

Author

💖 💪 🙅 🚩
abhishekgurjar
Abhishek Gurjar

Posted on August 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related