Skip to main content

How to Make a Form Field Required in HTML

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Add required to a form field, and the browser won't send the form while that field is empty. It stops, points at the field and tells the person what's missing.

Syntax​

Syntax
<input name="<name>" required>
<textarea name="<name>" required></textarea>
PartWhat it does
requiredThe field must have a value before the form can be sent. The attribute needs no value of its own.
<input>, <textarea>The fields it works on: text boxes, checkboxes, radio buttons and most other input types, plus text areas and lists made with select.

For a group of radio buttons, required on any one of them makes a choice in the group required. MDN's required page lists every type that supports it.

The Example​

The example is a feedback form with two radio buttons, Good and Bad, a Comment text area and a Send button. Both the rating and the comment are required.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding the question How was the event?, two required radio buttons named rating with the values good and bad, a label Comment, a required text area named comment, and a Send button.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Feedback</title>
</head>
<body>
<form>
<p>How was the event?</p>
<label><input type="radio" name="rating" value="good" required> Good</label>
<label><input type="radio" name="rating" value="bad" required> Bad</label>
<p><label for="comment">Comment</label></p>
<textarea id="comment" name="comment" required></textarea>
<p><button type="submit">Send</button></p>
</form>
</body>
</html>

Step 2: Send the Empty Form​

Double-click index.html to open it in your browser, and click Send without choosing anything:

The page in Chromium after clicking Send: a message bubble under the Good radio button says Please select one of these options.

The form wasn't sent: the address still ends with index.html.

Step 3: Choose a Rating and Send Again​

Click Good, then click Send:

The page in Chromium: Good is selected, the empty Comment box has a blue outline, and a message bubble under it says Please fill in this field.

Now the browser points at the comment. The words in the bubbles come from the browser, in its own language: this Chromium runs in British English. The screenshots come from Chromium 159, the open-source browser that Chrome is built on.

Step 4: Fill In the Comment​

Type Great talk into the comment box and click Send. This time the form is sent, and the address ends with index.html?rating=good&comment=Great+talk.

What Happened​

  • The browser checked the required fields in order before sending: first the rating, then the comment.
  • Each time it found an empty one, it stopped, showed one message next to it and didn't send the form.
  • When both had values, it sent the form as usual.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting required to reject a comment of only spacesSpaces count as a value: with three spaces in the comment, the form passed the checkCheck the text in a script or on the server too
Relying on required alone to keep bad data outThe check runs in the browser, and it can be switched off: with novalidate on the form, the empty form was sent as ?comment=Check the values again on the server
Putting required on only some radio buttons of a group, expecting the others to stay optionalThe whole group becomes required: with required on Good only, the form still couldn't be sent until Good or Bad was chosenPut required on every radio button in the group, or on none, so the code says what it does

Troubleshooting​

SymptomLikely causeFix
The form won't send and a bubble points at a fieldThe field is required and emptyFill it in
The form is sent with an empty fieldThe field doesn't have required, or the form has novalidateAdd required, or remove novalidate
A field full of spaces passes the checkrequired only checks that there's a valueCheck the content in a script or on the server

FAQ​

How do I turn the checks off while testing?​

Add novalidate to the form: <form novalidate>. The browser then sends the form without checking. The empty form went out as ?comment=, with no rating at all.

Why is the rating missing from the address when nothing is chosen?​

A radio group sends nothing when no option is selected, so rating only appears once one is chosen.

Does required work on a checkbox?​

Yes. A required checkbox has to be ticked before the form can be sent, which suits a box such as I accept the terms. Unticked, it stopped the form with the message Please tick this box if you want to proceed., and once it was ticked, the form passed the check.

Conclusion​

required makes the browser check that a field has a value before it sends the form, and it shows the person which field is missing. It doesn't check what the value is, and it runs only in the browser, so check the data again wherever you store it.

Additional Resources​