Popular Searches
Popular Course Categories
Popular Courses

Forms

28 mins HTML5 & Semantic Markup

Forms in React JS

Course: React JS Training

Topic: Forms

Course Link: React JS Training

Demo Link: Register for a Course Demo


1. Introduction: Why Forms Matter

Almost every useful website needs something from its visitors. A shopping site needs a delivery address, an email service needs a password, a job portal needs a resume, and even a simple blog needs a search box. All of this collecting of information happens through forms, which makes forms one of the most used building blocks on real web pages.

Forms are also the point where a page stops being static and starts reacting to people. Until now, HTML has mostly described content; a form waits for input, checks it, and sends it somewhere. That idea of collecting data and responding to it is exactly what you will later rebuild in React with controlled components and form events, so a solid grip on plain HTML forms now will pay off for the rest of this course.

  • A login box is a form.
  • A search box is a form.
  • A "contact us" section is a form.
  • A checkout page is mostly one long form.

2. What Is a Form? A Simple Definition

A form is a section of a web page that collects information from the user and hands that information over, either to a server or to a script running in the browser. In HTML, a form is created with the <form> element, which acts as a container for input controls such as text boxes, checkboxes, dropdown lists and buttons.

Think of a paper admission form at a college office. The sheet itself is just printed fields; the student fills it in, and the office collects the completed sheet and processes it. An HTML form works the same way: the browser draws the fields, the user fills them in, and on submit the data is packaged and sent off for processing.

3. Key Terms for Working with Forms

These words appear again and again in form work, in HTML documentation and later in React form handling, so it is worth fixing their meanings now.

TermMeaning in plain words
Form control (field)Any element the user can interact with inside a form, such as an input, select or textarea.
LabelThe visible text that tells the user what a field is for, connected to the field with the label element.
name attributeThe key under which a field's data is sent. A control without a name is not submitted at all.
value attributeThe data a control carries. For text inputs it is what the user typed; for checkboxes and radios it is the fixed value you set in the HTML.
SubmitThe act of sending the collected data onward, triggered by a submit button or the Enter key.
actionThe URL that receives the submitted data.
methodThe HTTP verb used to send the data, usually get or post.
Name/value pairThe smallest unit of submitted data, for example city=Mumbai. A form submits a list of these pairs.
ValidationChecking that the entered data is acceptable before it is used, for example that an email looks like an email.
FocusThe state of a control currently receiving keyboard input. Clicking a label moves focus to its field.

4. The Anatomy of a Form: Syntax and Structure

The basic skeleton

Every form follows the same pattern: a <form> wrapper, one or more labelled controls, and a submit button.

<form action="/welcome" method="get">
  <label for="visitor">Your name</label>
  <input type="text" id="visitor" name="visitor">
  <button type="submit">Say hello</button>
</form>

Submit this with the name "Asha" and the browser navigates to /welcome?visitor=Asha. Three things made that possible: the action said where to go, the method said how to send, and the name attribute said what to call the value.

Attributes of the form element

AttributeWhat it does
actionThe URL that handles the submitted data. If omitted, the form submits to the current page's URL.
methodget or post. Defaults to get when not written.
targetWhere the response opens: _self in the same tab (default) or _blank in a new tab.
autocompleteHints whether the browser may offer saved values, for example autocomplete="on" or off.
enctypeHow the data is encoded for post. Use multipart/form-data when the form includes file uploads.
novalidateSwitches off the browser's built-in validation for this form.

Choosing between GET and POST

AspectGETPOST
Where the data travelsAppended to the URL as a query stringInside the request body, hidden from the URL
Visible to the userYes, in the address barNo
Can be bookmarked or refreshed safelyYesThe browser warns before resending
Suitable for files or long textNo, URLs have practical length limitsYes
Typical useSearch boxes, filters, navigation by parametersSign-ups, logins, uploads, anything that changes data

5. How a Form Submission Works, Step by Step

  1. The user fills in the fields. As they type, each control holds a current value.
  2. The user presses a submit button (or Enter inside a single-input form). The browser fires the submit action.
  3. The browser walks through the form and builds name/value pairs from every control that has a name. Controls without a name are skipped entirely, which is a very common beginner bug.
  4. Un-ticked checkboxes and unselected radio groups contribute nothing; a ticked checkbox sends its value.
  5. The method decides the packaging. With get, the pairs are joined with & and appended to the action URL. With post, the pairs are placed in the request body.
  6. The browser sends the request to the action URL. A server, or later in this course a JavaScript handler, receives the pairs and responds, for example by loading a new page or returning a confirmation.

For a search form with two fields, a GET submission produces a URL like this:

/search?course=react&timing=evening

Everything after the question mark is the query string: course=react and timing=evening are the name/value pairs joined by &.

6. Your First Form: Text Inputs

A minimal working example

The input element is the workhorse of forms. It is a void element, so it has no closing tag, and its behaviour is chosen entirely by the type attribute.

<form action="/signup" method="post">
  <label for="email">Email</label>
  <input type="email" id="email" name="email" required>

  <label for="pass">Password (at least 8 characters)</label>
  <input type="password" id="pass" name="password" minlength="8">

  <label for="age">Age</label>
  <input type="number" id="age" name="age" min="16" max="60">

  <button type="submit">Create account</button>
</form>

Notice that type="email" and type="number" are not just cosmetic. The browser gives each a suitable on-screen keyboard on mobile devices and applies basic checks when the user submits, rejecting an email without an @ or an age outside the min and max range.

Useful input types

TypeMeant forWhat the browser adds
textGeneral single-line textPlain text field (the default type)
emailEmail addressesFormat check, email-friendly mobile keyboard
passwordSecretsCharacters shown as dots
numberNumeric valuesSpinner arrows, non-digits rejected
telPhone numbersTelephone keypad on mobile
urlWeb addressesBasic URL shape check
searchSearch boxesA clear button in many browsers
dateA calendar dayA date picker widget
colorA colour choiceA colour picker widget
rangeAn approximate valueA slider between min and max

7. Labels, Placeholders and Accessible Fields

Why every input needs a label

A field without a visible name forces the user to guess, and forces screen readers to announce nothing useful. The label element fixes both problems. The most common pattern connects the label to the field through the for attribute pointing at the field's id:

<label for="city">Which city are you based in?</label>
<input type="text" id="city" name="city">

A second, equally valid pattern wraps the input inside the label, in which case no for and id are needed:

<label>
  Which city are you based in?
  <input type="text" name="city">
</label>

Both patterns give the same benefits: clicking or tapping the label text focuses the field, and assistive technology announces the label when the field receives focus.

Label versus placeholder

AspectLabelPlaceholder
PurposeNames the field permanentlyShows a temporary hint inside the field
Visibility while typingAlways visibleDisappears as soon as the user types
Read by screen readersYes, as the field's nameUnreliable across technologies
Good forEvery field, alwaysShort format examples, such as "DD/MM/YYYY"

The practical rule: never use a placeholder as the only label. Once the user starts typing, the instruction vanishes, and there is nothing left to check against.

8. Checkboxes and Radio Buttons

Checkboxes: any number of choices

Checkboxes let the user pick zero, one or many options. Give every checkbox in a group the same name and a different value, so the server receives a list of the ticked values.

<p>Which days can you attend?</p>
<label><input type="checkbox" name="days" value="mon"> Monday</label>
<label><input type="checkbox" name="days" value="wed"> Wednesday</label>
<label><input type="checkbox" name="days" value="fri" checked> Friday</label>

If the user ticks Monday and Friday, the submission carries days=mon and days=fri. The checked attribute sets the state that is present when the page loads.

Radio buttons: exactly one choice

Radio buttons look similar but behave differently: radio buttons that share the same name form a group, and the user can select only one of them. This grouping by name is the whole mechanism, so a typo in one name quietly breaks the group.

<p>Preferred timing</p>
<label><input type="radio" name="timing" value="morning"> Morning</label>
<label><input type="radio" name="timing" value="evening" checked> Evening</label>
<label><input type="radio" name="timing" value="weekend"> Weekend</label>

Choosing Evening automatically deselects any other option in the timing group, and the submitted pair is timing=evening.

9. Dropdown Lists with Select and Option

When there are more than about five options, a dropdown saves space. The select element contains one option per choice, and the value attribute of the chosen option is what gets submitted.

<label for="track">Choose a course</label>
<select id="track" name="track">
  <optgroup label="Web development">
    <option value="html">HTML and CSS</option>
    <option value="react" selected>React JS</option>
  </optgroup>
  <optgroup label="Data">
    <option value="sql">SQL Basics</option>
  </optgroup>
</select>
  • The selected attribute marks the option chosen when the page loads; without it, the first option is the default.
  • optgroup gives the list readable headings without being selectable itself.
  • Adding the multiple attribute to the select turns it into a list where several options can be picked at once.
  • If an option has no value attribute, the text between the option tags is submitted instead.

10. Textareas, File Inputs and Hidden Fields

Textarea for long text

The textarea element accepts multi-line text such as messages, reviews or descriptions. Unlike input, it is not a void element: it has an opening and closing tag, and its initial content is written between the tags.

<label for="message">Your question</label>
<textarea id="message" name="message" rows="5" cols="40"
  placeholder="Type your question here..."></textarea>

The rows and cols attributes suggest the visible size, and users can usually drag the corner to resize it.

File uploads

An input with type="file" opens the operating system's file picker. Two details matter: the form must use method="post" with enctype="multipart/form-data", otherwise the browser cannot transmit the file contents, and the accept attribute filters what the picker offers.

<form action="/apply" method="post" enctype="multipart/form-data">
  <label for="resume">Upload your resume</label>
  <input type="file" id="resume" name="resume" accept=".pdf,.doc,.docx">
  <button type="submit">Apply</button>
</form>

Adding the multiple attribute lets the user pick several files in one go.

Hidden fields

A hidden input carries a fixed value that the user never sees or edits, but which is still submitted with the form. It is commonly used to attach context, such as which page sent the data.

<input type="hidden" name="source" value="website">

11. Buttons: Submit, Reset and Plain Buttons

The button element's type attribute decides what pressing it does, and the default type is submit, which surprises many beginners: a plain <button> inside a form will submit it.

TypeBehaviour when clickedTypical use
submitSends the form data to the action URLSaving, registering, searching
resetReturns all controls to their initial valuesA "clear the form" convenience
buttonDoes nothing by itself; JavaScript gives it behaviourShow/hide sections, calculations, previews
<form action="/search" method="get">
  <label for="q">Search courses</label>
  <input type="search" id="q" name="q">
  <button type="submit">Search</button>
  <button type="reset">Clear</button>
  <button type="button">Show filters</button>
</form>

A button element can contain rich content such as text plus an icon, which makes it more flexible than its older alternative <input type="submit">, whose visible text can only come from its value attribute. Both submit the form; prefer button in new work.

12. Grouping Fields with Fieldset and Legend

Long forms become much easier to read when related fields are grouped. The fieldset element draws a box around a group, and the legend element gives the group a caption. This is semantic markup doing real work: the grouping is announced to screen readers, it can be styled as a unit, and adding disabled to the fieldset disables every control inside it at once.

<fieldset>
  <legend>Shipping address</legend>
  <label for="street">Street</label>
  <input type="text" id="street" name="street">

  <label for="pin">PIN code</label>
  <input type="text" id="pin" name="pin" inputmode="numeric">
</fieldset>

13. A Realistic Worked Example: Course Registration Form

The following form brings together everything on this page: two fieldsets, several input types, a dropdown, radios, checkboxes, a textarea, a hidden field and both button types.

<form action="/register" method="post">
  <fieldset>
    <legend>Student details</legend>

    <label for="fullname">Full name</label>
    <input type="text" id="fullname" name="fullname" required>

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

    <label for="phone">Phone</label>
    <input type="tel" id="phone" name="phone">
  </fieldset>

  <fieldset>
    <legend>Course preferences</legend>

    <label for="track">Course</label>
    <select id="track" name="track">
      <option value="react">React JS Training</option>
      <option value="python">Python Programming</option>
    </select>

    <p>Preferred timing</p>
    <label><input type="radio" name="timing" value="morning"> Morning</label>
    <label><input type="radio" name="timing" value="evening" checked> Evening</label>

    <p>Extras</p>
    <label><input type="checkbox" name="extras" value="recordings"> Recorded sessions</label>
    <label><input type="checkbox" name="extras" value="notes"> Printed notes</label>

    <label for="goals">What do you want to build?</label>
    <textarea id="goals" name="goals" rows="4"></textarea>
  </fieldset>

  <input type="hidden" name="source" value="website">

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

Reading it piece by piece:

  • Each labelled control pairs a for with an id, so clicking the text focuses the field and screen readers announce it correctly.
  • Every submitted control has a name; the two p elements are only captions for the radio and checkbox groups, so they carry no data.
  • The required attribute on the name and email fields makes the browser block submission until they are filled.
  • The radios share the name timing, so exactly one timing reaches the server; the checkboxes share extras, so any combination reaches it.
  • The hidden field silently adds source=website to every submission.
  • Because the method is post, the pairs travel in the request body rather than in the URL, which is the right choice for a sign-up style form.

If this form were submitted with the name "Asha", the email filled in, Evening selected and Recorded sessions ticked, the server would receive these pairs: fullname=Asha, email=..., track=react, timing=evening, extras=recordings and source=website. That list of name/value pairs is the entire payload of a form, and every server language and every JavaScript framework, React included, ultimately works with exactly this shape of data.

14. Advanced Attributes That Improve Real Forms

Part 1 covered the everyday attributes such as type, name, id and value. Real production forms lean on a second layer of attributes that make typing faster, constrain the data and stop bad submissions before any JavaScript or server code gets involved.

AttributeWhat it doesTypical use
autocompleteTells the browser which saved value fits the field, for example email, current-password or postal-codeLogin and checkout forms, faster filling, fewer typos
inputmodeChooses the on-screen keyboard for a text input, for example numericPINs and OTPs typed into a type="text" field
patternA regular expression the value must match to count as validPIN codes, usernames, order references
min, max, stepBounds and increments for number, date and range inputsAge limits, quantity pickers
maxlengthMaximum number of characters the user can typeComments boxes, name fields
autofocusPuts focus on the control when the page loadsOne field per page at most, such as a search box
multipleAllows several values in one controlFile pickers and type="email" recipient lists
listConnects the input to a datalist of suggestionsText boxes that benefit from hints
readonlyShows a value the user cannot edit, but the value is still submittedGenerated order or reference numbers
disabledSwitches the control off entirely; it cannot be focused and is not submittedFields that do not apply yet

The datalist element: a text box with suggestions

A datalist sits between a free text box and a dropdown. The user may type anything, but the browser offers matching suggestions from the list as they type. Link the input to the list with the list attribute, whose value is the id of the datalist.

<label for="city">City</label>
<input type="text" id="city" name="city" list="city-list">
<datalist id="city-list">
  <option value="Mumbai">
  <option value="Pune">
  <option value="Hyderabad">
</datalist>

Unlike a select, nothing forces the user to pick from the list, so this suits fields where common answers exist but exceptions must stay possible.

readonly versus disabled

Aspectreadonlydisabled
User can edit the valueNoNo
Field can receive focusYesNo
Value is submitted with the formYesNo
Best forData shown for confirmationFields that are temporarily out of play
<label for="ref">Reference number</label>
<input type="text" id="ref" name="ref" value="JS-0042" readonly>

<label for="coupon">Coupon code</label>
<input type="text" id="coupon" name="coupon" disabled>

If you later enable the coupon field with JavaScript, remember that until then its value would never reach the server even if it contained text.

15. Built-in Validation: Letting the Browser Check the Data

HTML5 forms include a built-in checker. When the user tries to submit, the browser inspects every control against its attributes. If something fails, the submission is stopped, the first invalid field receives focus, and a message explains the problem. All of this happens without a single line of JavaScript.

<form action="/join" method="post">
  <label for="user">Username (4 to 12 letters)</label>
  <input type="text" id="user" name="username"
         required minlength="4" maxlength="12" pattern="[A-Za-z]+">

  <label for="pin">6-digit PIN</label>
  <input type="text" id="pin" name="pin"
         required pattern="[0-9]{6}" inputmode="numeric">

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

Reading the checks in plain words:

  • required means the field cannot be left empty.
  • minlength and maxlength bound the length of the text.
  • pattern="[A-Za-z]+" means one or more letters, nothing else; the plus sign means "one or more of the thing before it".
  • pattern="[0-9]{6}" means exactly six digits; the number in braces is an exact count.
  • inputmode="numeric" does not validate anything; it simply opens a number keypad on touch devices, which makes the six-digit PIN far easier to type.

Two practical notes. First, adding the novalidate attribute to the form element switches these checks off, which is useful while testing or when you plan to validate with JavaScript later in the course. Second, browser validation is a convenience for honest users, not a security wall: a crafted request can skip the browser entirely, so the server must always check the data again.

16. Real-World Worked Examples: Login and Search

A login form

<form action="/login" method="post">
  <label for="login-email">Email</label>
  <input type="email" id="login-email" name="email"
         required autocomplete="email">

  <label for="login-pass">Password</label>
  <input type="password" id="login-pass" name="password"
         required autocomplete="current-password">

  <button type="submit">Log in</button>
</form>

Three deliberate choices: the method is post so the credentials never appear in the URL, the password type hides the characters on screen, and the autocomplete values let password managers and browsers fill the fields correctly.

A search form

<form action="/search" method="get" role="search">
  <label for="q">Search courses</label>
  <input type="search" id="q" name="q">
  <button type="submit">Search</button>
</form>

Here get is the right method: a search does not change anything on the server, and the resulting URL such as /search?q=react can be bookmarked, shared and refreshed without any warning. The role="search" attribute marks the form as a search landmark for assistive technology.

17. Common Mistakes and the Correct Way

Forgetting the name attribute

<!-- Wrong: the value never reaches the server -->
<input type="email" id="email" required>

<!-- Correct: id for labels, name for submission -->
<label for="email">Email</label>
<input type="email" id="email" name="email" required>

The id connects the label and is used by CSS and JavaScript; the name is the key in the submitted name/value pair. A control without a name is silently skipped during submission.

Trusting a button's default type

<!-- Wrong: this "Add another subject" button also submits the form -->
<button>Add another subject</button>

<!-- Correct: plain buttons must say so explicitly -->
<button type="button">Add another subject</button>

The default type of a button inside a form is submit. Any button that is meant to be handled by JavaScript needs an explicit type="button".

Radio buttons that fail to group

<!-- Wrong: three independent toggles, all selectable -->
<label><input type="radio" name="mode-a" value="online"> Online</label>
<label><input type="radio" name="mode-b" value="offline"> Offline</label>

<!-- Correct: one shared name creates one group -->
<label><input type="radio" name="mode" value="online"> Online</label>
<label><input type="radio" name="mode" value="offline"> Offline</label>

Grouping is created purely by sharing the same name. One typo in one name quietly breaks the "exactly one choice" behaviour.

Quick reference of further mistakes

MistakeWhy it failsCorrect way
Using GET for a password or payment formThe data appears in the URL, browser history and server logsUse method="post" for anything sensitive or anything that changes data
File upload without the right encodingThe server receives no file contentmethod="post" together with enctype="multipart/form-data"
Duplicate id values on a pageA label's for connects to the wrong fieldKeep every id unique per page
Leaving action="#" in finished workThe form reloads the same page and the data goes nowherePoint action at a real handler, or handle submission in JavaScript
Assuming browser validation is enoughRequests can be crafted that skip the browser entirelyRepeat every important check on the server
Using type="text" everywhereWrong mobile keyboards and weaker built-in checksPick the specific type: email, tel, url, number, date

18. Best Practices for Building Forms

  • Give every field a real label, joined by for and id or by wrapping; never rely on a placeholder alone.
  • Give every submitted control a meaningful name, because that name becomes the key the server and your future React code will read.
  • Choose the most specific type available, and add inputmode when a text field needs a special keyboard.
  • Use get for safe retrievals such as search and filters, and post for sign-ups, logins, uploads and anything that changes data.
  • Declare validation with attributes such as required, min, maxlength and pattern, then mirror the important checks on the server.
  • Group related fields in fieldset with a legend, which helps sighted users, screen reader users and your own maintenance.
  • Use standard autocomplete values so browsers and password managers can help the user.
  • Mark required fields visibly in the design, not only with the required attribute.
  • Use autofocus sparingly, at most once per page, because stealing focus disorients keyboard and screen reader users.
  • Test each form with the keyboard alone: tab through it, submit it empty, and confirm the focus lands on the first invalid field.

19. Practical Project: A Course Feedback Form, Step by Step

This small project uses almost everything from both parts of this page. The goal is a feedback form for a training course that validates itself before submitting.

Step 1: List the fields on paper first

Decide what you need before writing tags: name, email, course chosen, a rating, free-text comments and an optional newsletter tick box. Grouping is already visible in this list: "about you" and "your feedback".

Step 2: Write the page skeleton and the form element

Create a minimal HTML page and add a form with method="post", because feedback changes data on the server and should not appear in a URL.

Step 3: Add two fieldsets with legends

One fieldset for personal details and one for the feedback itself, each opened with a legend.

Step 4: Add the controls with labels and validation

Text inputs for name and email, a select for the course, radio buttons for the rating, a textarea with a maxlength for comments, and a checkbox for the newsletter. Make the rating group required so a submission without a rating is blocked.

Step 5: Add the submit button

One button type="submit". No reset button here: destroying a half-written review by accident is worse than leaving old text in place.

Step 6: Test the behaviour, not just the looks

Submit while empty and watch the browser block you; submit with a rating and watch the request carry the pairs.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Course Feedback</title>
</head>
<body>
  <h1>Course Feedback</h1>

  <form action="/feedback" method="post">
    <fieldset>
      <legend>About you</legend>

      <label for="fb-name">Name</label>
      <input type="text" id="fb-name" name="name" required>

      <label for="fb-email">Email</label>
      <input type="email" id="fb-email" name="email" required>
    </fieldset>

    <fieldset>
      <legend>Your feedback</legend>

      <label for="fb-course">Course</label>
      <select id="fb-course" name="course">
        <option value="html">HTML5 &amp; Semantic Markup</option>
        <option value="css">CSS3 &amp; Responsive Design</option>
        <option value="js">JavaScript Fundamentals</option>
      </select>

      <p>How would you rate the sessions?</p>
      <label><input type="radio" name="rating" value="5" required> Excellent</label>
      <label><input type="radio" name="rating" value="4"> Good</label>
      <label><input type="radio" name="rating" value="3"> Average</label>

      <label for="fb-comments">Comments</label>
      <textarea id="fb-comments" name="comments" rows="4" maxlength="500"></textarea>

      <label><input type="checkbox" name="newsletter" value="yes">
        Send me practice exercises by email</label>
    </fieldset>

    <button type="submit">Send feedback</button>
  </form>
</body>
</html>

When it works, you should observe two behaviours. Submitting with empty name or email fields, or with no rating chosen, is blocked with a message next to the first problem. A completed submission sends pairs such as name=Asha, email=..., course=html, rating=4, comments=... and, if ticked, newsletter=yes. Notice that an unticked newsletter checkbox sends nothing at all, which is exactly the checkbox behaviour from part 1.

20. Practice Exercises

  1. Build a pizza order form: radio buttons for size, checkboxes for toppings, a number input for quantity with min and max, and a textarea for delivery instructions. Submit with GET and read the pairs out of the URL.
  2. Take any form you have written and replace one dropdown with a datalist. Confirm that you can still type a value that is not in the list.
  3. Add a PIN field that accepts exactly four digits using pattern and inputmode, then deliberately type letters and watch the browser reject the submission.
  4. Break a form on purpose: remove the name attributes from two fields, submit, and observe which pairs disappear. Then restore them.
  5. Build the same small form twice, once with method="get" and once with method="post", and compare what appears in the address bar after submitting.
  6. Unplug the mouse and complete one of your forms using only the Tab, Space and Enter keys. Fix anything that traps or skips focus.

21. Interview Questions and Answers

What is the difference between the name and id attributes on a form control?

The id identifies the element within the page: labels point at it through for, and CSS and JavaScript select by it. The name is the key under which the control's value is submitted. Both are usually needed, and they serve completely separate purposes.

What happens if a form control has no name attribute?

It is excluded from submission entirely. The browser builds name/value pairs only from controls that have a name, so the user's input in that field is silently lost.

When should a form use GET and when POST?

Use GET when the submission only retrieves data, such as search or filters, and the parameters are safe to show in the URL. Use POST when the data is sensitive, large, or changes something on the server, such as sign-ups, logins and file uploads.

What does the for attribute on a label do?

It links the label to the control whose id it names. Clicking the label then focuses the control, and screen readers announce the label as the control's accessible name. Wrapping the control inside the label achieves the same link without for and id.

How does built-in HTML5 validation work? Name three attributes.

When a submit is attempted, the browser checks each control against its validation attributes and blocks submission if any fail, focusing the first invalid field. Common attributes are required, minlength or maxlength, min and max, and pattern, while types such as email and url add their own checks.

What is the pattern attribute?

It holds a regular expression that the value must match. For example pattern="[0-9]{6}" demands exactly six digits, and pattern="[A-Za-z]+" demands one or more letters. It works on text-like inputs and is checked at submission time.

What is the difference between readonly and disabled?

A readonly field cannot be edited but can be focused and its value is submitted. A disabled field is switched off: it cannot be focused and its value is not submitted at all.

Why must a file upload form use POST with multipart/form-data?

GET has no practical way to carry file content, and the default form encoding only handles simple name/value pairs. multipart/form-data packages the file data alongside the other fields so the server can reconstruct the uploaded file.

What is the default type of a button element inside a form, and why does it matter?

The default is submit, so any plain button inside a form submits it when clicked. Buttons intended for JavaScript behaviour must set type="button" explicitly, or they will trigger unwanted submissions.

How does a datalist differ from a select?

A select restricts the user to the listed options. A datalist only suggests: the user types into a normal text input and may enter any value, with the list appearing as optional autocomplete hints.

What do checkboxes and radio groups submit differently?

Each checkbox is independent: only ticked boxes contribute a pair, using their shared name and their individual value. Radio buttons sharing a name form a group where exactly one option can be selected, and that single value is submitted.

How does HTML form knowledge carry over to building forms in React?

Directly. React forms still use the same elements, types, labels and validation attributes; what changes is who owns the current value. In React you typically keep the value in component state and handle the submit event yourself instead of letting the browser navigate. The name/value pair model you learned here remains the shape of the data you work with.

22. Quick Revision Checklist

  • A form is a form element containing labelled controls and a submit button.
  • action says where data goes; method says how; name says what each value is called.
  • GET appends pairs to the URL; POST puts them in the request body.
  • Controls without a name are never submitted.
  • Every field needs a real label linked by for and id, or by wrapping.
  • Checkboxes allow many selections; radios sharing a name allow exactly one.
  • select restricts to listed options; datalist only suggests.
  • textarea holds multi-line text and its default value goes between its tags.
  • File uploads need method="post" and enctype="multipart/form-data".
  • A plain button defaults to type="submit"; add type="button" for JavaScript-only buttons.
  • readonly values are submitted; disabled values are not.
  • Validation attributes: required, minlength, maxlength, min, max, pattern; novalidate switches the checks off.
  • Always re-validate important data on the server; browser checks are a convenience, not a guarantee.

23. Official Documentation and Further Reading

No official HTML specification pages were confirmed by the research behind this course, so the reading below stays within the course's curated list and points forward to where form knowledge is applied in React, the framework this training builds towards.

Conclusion

Forms are where a web page starts listening. You now know the full anatomy: the form wrapper with its action and method, the family of input types, labels that name fields for people and screen readers alike, grouping with fieldset and legend, and the validation attributes that let the browser catch mistakes before any code runs. Just as important, you know the failure modes: the missing name, the accidental submit button, the ungrouped radios, the GET request carrying a password.

Everything here was written in plain HTML with no JavaScript, and that is deliberate. When you reach the DOM module you will intercept the submit event and validate with your own code, and when you reach React you will rebuild these same fields as components with state. Both of those steps stand on the name/value pair model and the accessibility habits you practised on this page, so keep the feedback form project handy: it is the specimen you will dissect again later in the course.

Learn more in the React JS Training course, or register for a course demo.

Previous Next
whatsapp