HTML Required Fields

Required Fields in HTML Forms

Not every form field is optional — sometimes you absolutely need a user’s name, email, or opinion. That’s where the required attribute comes in. It's the simplest way to add client-side form validation, and it works right out of the box without JavaScript.

What Does required Do?

The required attribute ensures a user cannot submit the form unless the field is filled out. If they try, the browser automatically shows a validation message — no extra coding needed.

Using required with Text Inputs

<form>
  <label for="fruit">Favorite Fruit (required):</label><br>
  <input type="text" id="fruit" name="fruit" required><br>
  <input type="submit" value="Submit">
</form>
Favorite Fruit (required): [           ]
[Submit]

Try clicking "Submit" without typing anything — the browser will stop you with a warning.

Using required with Other Input Types

You can apply required to many form elements: text, email, checkboxes, select, and even textareas.

Email Example:

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

Textarea Example:

<label for="comment">Your Comment:</label><br>
<textarea id="comment" name="comment" rows="4" cols="40" required></textarea>

Select Dropdown Example:

<label for="fruit-choice">Pick one fruit:</label><br>
<select id="fruit-choice" name="fruit" required>
  <option value="" disabled selected>-- Choose one -->
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="cherry">Cherry</option>
</select>

Required Checkboxes

To ensure a user accepts terms or confirms a checkbox, add required to the checkbox.

<input type="checkbox" id="agree" name="terms" required>
<label for="agree">I agree to the terms</label>
☐ I agree to the terms

The form won't submit unless the box is checked.

Real-World Example: Required Fields in a Survey Form

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

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

  <label for="comment">Comment:</label><br>
  <textarea id="comment" name="comment" rows="4" cols="50" required></textarea><br><br>

  <input type="submit" value="Send">
</form>
Name: [             ]  
Email: [             ]  
Comment:
[                                    ]
[                                    ]
[                                    ]
[                                    ]

[Send]

Try submitting the form without filling a field. Each required field will be highlighted until completed.

Browser Behavior

Modern browsers handle required validations automatically. They show messages, highlight empty fields, and prevent submission — no extra scripting required.

Best Practices

  • Only mark fields as required when absolutely necessary
  • Combine with clear <label> elements so users know what’s expected
  • Don’t use required on hidden fields — the user can’t see or interact with them

Summary

The required attribute gives you instant client-side validation. You’ve now learned to:

  • Add required to inputs, selects, textareas, and checkboxes
  • Use it to block form submission until all key fields are filled
  • Enhance user experience without writing any JavaScript

Check your understanding 1

What does the required attribute do when added to a form input?
Choose one answer. You can try again.

Answer and explanation

It makes the field mandatory before form submission

The required attribute forces users to fill out the field before submitting the form. If left empty, the browser prevents form submission and shows a validation message.

Check your understanding 2

Using the required attribute on an input field means users must provide a value before the form can be submitted.
Choose one answer. You can try again.

Answer and explanation

True

Correct. This is the core functionality of required — it enforces client-side validation without any scripting.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. Student's full name
  2. Parent's phone number
  3. Optional feedback
  4. Class selection
Which input fields are good candidates for the required attribute in a school registration form?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

Essential fields like names, phone numbers, and class selections should be required. Optional feedback fields don’t need the required attribute.

Check your understanding 4

What will happen if a required field is left empty and the user clicks the submit button?
Choose one answer. You can try again.

Answer and explanation

The form is blocked from submitting until the required field is filled

HTML5 prevents the form from submitting and displays a browser-generated message asking the user to fill in the required field.

Check your understanding 5

You must use JavaScript to validate required fields in modern HTML forms.
Choose one answer. You can try again.

Answer and explanation

False

You can use the required attribute alone for simple client-side validation without any JavaScript.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <input type="text" name="student_name" required>
  2. <textarea name="remarks" required></textarea>
  3. <select name="class" required><option>--Choose--</option></select>
  4. <input type="file" required>
Which of the following are valid examples of using the required attribute?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

The first, second, and fourth options correctly apply the required attribute. The third one only works if the <select> has a non-empty default or JavaScript enforces logic — HTML alone won’t stop the default --Choose-- if it has a value.

What’s Next?

Coming up: Learn how to use Pattern Matching to validate inputs using regular expressions — for more control and smarter forms.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Input Pattern Attribute →