Google Advertisement

How to Create a Validated HTML Contact Form Using Required and Maxlength Attributes

Google Advertisement
🔥 Read with Full Features on Our Website

Learn step-by-step how to create a user-friendly contact form in HTML with built-in input validation using required and maxlength attributes. Perfect for beginners.

Published on 15 May 2025
By ttimesnow

Build a Contact Form with Input Validation Attributes (required, maxlength) in HTML

Creating a contact form is one of the first projects many beginners try in HTML. It allows users to send their name, email, message, and other information to a website owner.

🔥 Read with Full Features on Our Website

In this tutorial, you will build a simple contact form and learn how to use two powerful HTML validation attributes:

Let’s get started step-by-step! ✅


🔹 Step 1: Basic HTML Structure

First, every HTML page needs a basic structure.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Contact Us</title>
</head>
<body>

  <!-- Form will go here -->

</body>
</html>

🔍 Explanation:


🔹 Step 2: Add a Form Element

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

🔍 Explanation:


🔹 Step 3: Add Input Fields with Validation

Here’s a simple contact form:

<form action="#" method="post">
  <label for="name">Name:</label><br>
  <input type="text" id="name" name="name" required maxlength="50"><br><br>

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

  <label for="message">Message:</label><br>
  <textarea id="message" name="message" required maxlength="300"></textarea><br><br>

  <input type="submit" value="Submit">
</form>

🔍 Field-by-Field Explanation:

🧍 Name Input:

<input type="text" id="name" name="name" required maxlength="50">

📧 Email Input:

<input type="email" id="email" name="email" required maxlength="100">

💬 Message Textarea:

<textarea id="message" name="message" required maxlength="300"></textarea>

✅ Submit Button:

<input type="submit" value="Submit">

🧪 Built-in Validation in Action

When you try to submit the form without filling out the required fields, the browser will show an automatic message like:

“Please fill out this field.”

And if the input is too long, the browser will stop the user from typing further.

No JavaScript needed – this is pure HTML validation!


💡 Pro Tips


✅ Final Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Contact Us</title>
</head>
<body>
  <h1>Contact Form</h1>
  <form action="#" method="post">
    <label for="name">Name:</label><br>
    <input type="text" id="name" name="name" required maxlength="50"><br><br>

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

    <label for="message">Message:</label><br>
    <textarea id="message" name="message" required maxlength="300"></textarea><br><br>

    <input type="submit" value="Submit">
  </form>
</body>
</html>

🏁 Conclusion

Using required and maxlength in HTML makes your contact forms much more user-friendly and secure – and the best part? You don’t need JavaScript to do it!

❤️ Like 💬 Comment 🔗 Share
Google Advertisement
👉 View Full Version on Main Website ↗
Google Advertisement
👉 Read Full Article on Website