Google Advertisement

Step-by-Step Guide to HTML Contact Forms with Validation (required, maxlength)

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

Learn how to build a contact form in HTML using input validation attributes like required and maxlength. Step-by-step tutorial with code and easy explanation

Published on 15 May 2025
By Priya

Forms are the foundation of user interaction on websites. Whether it's signing up, logging in, or contacting support — forms are everywhere! In this tutorial, we’ll build a simple contact form in HTML and apply input validation using the required and maxlength attributes.

πŸ”₯ Read with Full Features on Our Website

You don’t need any JavaScript or backend setup to follow this guide. This is perfect for beginners who want to understand how HTML handles basic form validation without external code.


🧰 What We’ll Learn


πŸ”§ Step-by-Step Tutorial: Build the Contact Form

Let’s now dive into building the contact form step by step.


πŸ”Ή Step 1: Set up the Basic HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Contact Us Form</title>
</head>
<body>
  <!-- Contact form will go here -->
</body>
</html>

βœ… Explanation:


πŸ”Ή Step 2: Add the Form Element

Now, we’ll create the form element inside the <body> tag.

<form action="#" method="post">
  <!-- Form fields will go here -->
</form>

βœ… Explanation:

πŸ”Ή Step 4: Add Email Input Field (with Validation)

<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required maxlength="100">

βœ… Explanation:

Bonus: Modern browsers will automatically show an error if the user doesn’t enter a valid email.


πŸ”Ή Step 5: Add Message Field (Using <textarea>)

<label for="message">Your Message:</label>
<textarea id="message" name="message" required maxlength="300" rows="5" cols="30"></textarea>

βœ… Explanation:


πŸ”Ή Step 6: Add the Submit Button

<button type="submit">Send Message</button>

βœ… Explanation:


πŸ”Ή Full HTML Contact Form Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Contact Us Form</title>
</head>
<body>

  <h2>Contact Us</h2>
  <form action="#" method="post">
    
    <label for="name">Full Name:</label><br>
    <input type="text" id="name" name="name" required maxlength="50"><br><br>
    
    <label for="email">Email Address:</label><br>
    <input type="email" id="email" name="email" required maxlength="100"><br><br>
    
    <label for="message">Your Message:</label><br>
    <textarea id="message" name="message" required maxlength="300" rows="5" cols="30"></textarea><br><br>
    
    <button type="submit">Send Message</button>
    
  </form>

</body>
</html>

πŸ§ͺ How Does Validation Work?

Let’s break down how HTML handles form validation:

Attribute Purpose                     User Experience
                         required Ensures the field is not left empty    Shows error if user skips it
                       type="email Automatically checks for email format     Helps catch typos

 

This built-in validation reduces user mistakes without using JavaScript or external libraries.


🎯 Bonus Tips for a Better Contact Form

  1. Use placeholders:
    Add placeholder="Enter your full name" to inputs to give hints.

  2. Add CSS for better styling:
    Use simple CSS to improve spacing and readability.

  3. Accessibility matters:
    Always use <label> to describe each input field for screen readers.

  4. Test on multiple browsers:
    Try Chrome, Firefox, Safari, etc., to ensure consistent validation behavior.


βœ… Why Use required and maxlength?

Using these attributes makes your form:


πŸ“Œ Conclusion

You've now built a fully functional and validated contact form using just HTML! By applying required and maxlength, you improve form usability and ensure cleaner user input — all without writing a single line of JavaScript.

This foundational knowledge is critical whether you're building simple static pages or getting ready for more advanced front-end development with frameworks.


If you found this tutorial helpful, consider sharing it or bookmarking for later. Got questions or want to see a version with CSS and JavaScript enhancements? Drop a comment or check out our next tutorial.

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website