Open In App

Create a Contact Form using HTML CSS & JavaScript

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A contact form is a web form used to collect user information and messages, facilitating communication between visitors and site owners. It is essential for feedback, inquiries, and support. Create a contact form using HTML for structure, CSS for styling, and JavaScript for validation and submission.

Preview Image:

Web-capture_26-10-2023_123648_

Prerequisites

Approach

  • Create a HTML structure for the contact form component having proper id and classes for the styles.
  • Add a CSS file which contains all the styles related to the contact form component.
  • Add a JavaScript file having all the logic for validation and boundary checks.
  • Form Validations Conditions
    • Length of name should be 5 or more.
    • Length of subject should be 10 or more.
    • Length of number should be 10.
    • Email must include @ and length more than 6.
    • Message length must be 40 characters.

Then, link the JavaScript and CSS file to the HTML file.

Example: This example describes the basic implementation of the contact form using HTML, CSS, and JavaScript.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Contact Form</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <div class="contact-form-container">
        <h2>Contact Us</h2>
        <form id="contactForm">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" 
                       name="name" 
                       placeholder="Your Name" required>
                <span class="error-message" id="nameError"></span>
            </div>
            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" 
                       name="email" 
                       placeholder="Your Email" required>
                <span class="error-message" id="emailError"></span>
            </div>
            <div class="form-group">
                <label for="phone">Phone</label>
                <input type="tel" id="phone" 
                       name="phone" 
                       placeholder="Your Phone Number" required>
                <span class="error-message" id="phoneError"></span>
            </div>
            <div class="form-group">
                <label for="message">Message</label>
                <textarea id="message" 
                          name="message" 
                          placeholder="Your Message" 
                          rows="5" required></textarea>
                <span class="error-message" 
                      id="messageError"></span>
            </div>
            <button type="submit" 
                    class="submit-button">
              Send Message
            </button>
        </form>
    </div>
    <script src="scripts.js"></script>
</body>

</html>
CSS JavaScript

Output:

a2

Next Article

Similar Reads

three90RightbarBannerImg