Validation

Use built-in validation attributes before adding JavaScript.

When users fill out a form, they may accidentally enter incorrect information or leave important fields empty. Form validation helps ensure that the data entered is correct before the form is submitted.

For example, if a registration form requires an email address, validation can check whether the user entered a properly formatted email. If not, the browser can display an error message and prevent the form from being submitted.

What Is Form Validation?

Form validation is the process of checking user input before it is sent to a server.

Validation helps ensure that:

  • Required fields are completed.
  • Email addresses are valid.
  • Numbers are within an acceptable range.
  • Data follows a specific format.

By validating data early, websites can reduce errors and improve the user experience.

Why Is Form Validation Important?

Imagine an online registration form where users forget to enter their email address.

Without validation:

  • Incomplete information may be submitted.
  • The website may not work correctly.
  • Users may become frustrated.

With validation:

  • Errors are detected immediately.
  • Users receive helpful feedback.
  • Data quality improves.

Required Fields

The required attribute makes a field mandatory.

html
<form>
    <label>Name:</label>
    <input type="text" required>

    <button>Submit</button>
</form>

If the user leaves the field empty, the browser prevents submission and displays an error message.

Email Validation

The email input type checks whether the entered value looks like an email address.

html
<input type="email" required>

Valid input:

text
john@example.com

Invalid input:

text
johnexample.com

The browser automatically checks the format.

Number Validation

The min and max attributes can limit allowed values.

html
<input type="number" min="18" max="60">

In this case:

  • Values below 18 are not allowed.
  • Values above 60 are not allowed.

This is useful for age, quantity, and score fields.

Length Validation

The minlength and maxlength attributes control the number of characters allowed.

html
<input type="text" minlength="3" maxlength="20">

The user must enter between 3 and 20 characters.

Pattern Validation

The pattern attribute allows custom validation rules using regular expressions.

html
<input type="text" pattern="[A-Za-z]{3,}">

This example accepts only letters and requires at least three characters.

Pattern validation is useful for usernames, phone numbers, and custom formats.

Real-World Example

A simple registration form:

html
<form>
    <label>Name:</label>
    <input type="text" required>

    <label>Email:</label>
    <input type="email" required>

    <label>Age:</label>
    <input type="number" min="18" max="100">

    <button type="submit">
        Register
    </button>
</form>

This form ensures:

  • Name cannot be empty.
  • Email is valid.
  • Age falls within the allowed range.

Best Practices

When using form validation:

  • Mark important fields as required.
  • Use the correct input types.
  • Provide clear labels and instructions.
  • Keep validation rules simple.
  • Give users helpful error messages.

Good validation improves usability and reduces mistakes.

Summary

HTML form validation helps ensure that users enter correct and complete information before submitting a form. Attributes such as required, min, max, minlength, maxlength, and pattern allow browsers to automatically check user input. By using validation effectively, developers can create forms that are more reliable, user-friendly, and easier to use.

Let's learn with DevBrainBox AI