Skip to main content

HTML Forms & Inputs Cheatsheet

Compact reference for all form elements, input types, and validation attributes.


Form Structure

<form action="/submit" method="post" enctype="multipart/form-data" novalidate>
<fieldset>
<legend>Personal Info</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<button type="submit">Submit</button>
</fieldset>
</form>

Form Attributes

AttributeValuesDescription
actionURLWhere to send form data
methodget / postHTTP method
enctypeapplication/x-www-form-urlencoded / multipart/form-data / text/plainEncoding when submitting
autocompleteon / offEnable/disable autofill
novalidate(boolean)Bypass HTML5 validation
target_self / _blank / _parent / _topWhere to display response

Tip: Use enctype="multipart/form-data" when your form includes <input type="file">.


All Input Types

TypePurposeExample
textSingle-line text<input type="text">
emailEmail with validation<input type="email">
passwordMasked input<input type="password">
numberNumeric with spinner<input type="number" min="1" max="10">
telPhone number<input type="tel">
urlWeb address<input type="url">
searchSearch field<input type="search">
dateDate picker<input type="date">
timeTime picker<input type="time">
datetime-localDate + time<input type="datetime-local">
monthMonth and year<input type="month">
weekWeek number<input type="week">
colorColour picker<input type="color">
rangeSlider<input type="range" min="0" max="100">
fileFile upload<input type="file" accept="image/*">
hiddenSubmits invisible data<input type="hidden" name="id" value="123">
checkboxMultiple choice (on/off)<input type="checkbox">
radioSingle choice from group<input type="radio" name="opt" value="a">
submitSubmits the form<input type="submit" value="Send">
resetResets form to defaults<input type="reset" value="Clear">
buttonGeneric button (needs JS)<input type="button" value="Click">

Example: Key Input Types in Action

<!-- Text -->
<input type="text" name="username" placeholder="Username" required>

<!-- Email -->
<input type="email" name="email" placeholder="[email protected]">

<!-- Number with constraints -->
<input type="number" name="age" min="1" max="120" step="1">

<!-- Date -->
<input type="date" name="dob" min="1900-01-01" max="2010-12-31">

<!-- Range -->
<input type="range" name="volume" min="0" max="100" value="50">

<!-- File -->
<input type="file" name="resume" accept=".pdf,.docx" multiple>

Input Attributes

AttributeApplies ToDescription
type<input>Input type (text, email, etc.)
nameAllForm field name (used on submission)
valueAllDefault / submitted value
placeholdertext/search/tel/url/email/passwordHint text inside field
requiredAllField must be filled
readonlyAllField cannot be edited
disabledAllField is greyed-out and not submitted
min / maxnumber/range/date/timeConstrain range
minlength / maxlengthtext/password/search/tel/url/email/textareaConstrain character count
patterntext/password/search/tel/url/emailRegex validation
stepnumber/range/date/timeIncrement step
autocompleteAllOverride form-level autocomplete
autofocusAllFocus on page load (only one)
multipleemail/fileAllow multiple values
acceptfileRestrict file types
checkedcheckbox/radioPre-selected
selected<option>Pre-selected option
sizeselectVisible option count

Validation Attributes Reference

AttributeDescriptionExample
requiredField must have a value<input required>
minlength="3"Minimum characters<input minlength="3">
maxlength="20"Maximum characters<input maxlength="20">
min="18"Minimum numeric/date value<input type="number" min="18">
max="100"Maximum numeric/date value<input type="number" max="100">
pattern="[A-Z]+"Regex pattern<input pattern="[A-Za-z]+">
step="0.5"Allowed increment<input type="number" step="0.5">
type="email"Email format validation<input type="email">
type="url"URL format validation<input type="url">

Custom Validation Messages

<input
type="text"
name="username"
pattern="[A-Za-z0-9]{3,16}"
title="3–16 characters, letters and numbers only"
required
>

Select & Option

<label for="country">Country:</label>
<select id="country" name="country" required>
<option value="">— Select —</option>
<option value="in">India</option>
<option value="us">United States</option>
<option value="uk" selected>United Kingdom</option>
</select>

<!-- Multiple select -->
<select name="skills" multiple size="4">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
</select>

<select> Attributes

AttributeDescription
multipleAllow multi-select (Ctrl+click)
sizeVisible options (makes it a list)
requiredMust select an option
disabledDisable the dropdown

Textarea

<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" cols="50"
placeholder="Your message..." maxlength="500" required></textarea>

<textarea> Attributes

AttributeDescription
rowsVisible text lines
colsVisible character width
maxlengthMax character count
placeholderHint text
readonlyCannot edit
disabledGreyed-out, not submitted
requiredMust have content

Button

TypeClassDescription
submit<button type="submit">Submits the form (default)
reset<button type="reset">Resets all fields to defaults
button<button type="button">Does nothing — attach JS
<button type="submit">Send</button>
<button type="reset">Clear</button>
<button type="button" onclick="alert('Clicked!')">Click Me</button>

Complete Form Example

<form action="/register" method="post" autocomplete="on">
<fieldset>
<legend>Account Details</legend>

<label for="name">Full Name *</label>
<input type="text" id="name" name="name" placeholder="John Doe" required>

<label for="email">Email *</label>
<input type="email" id="email" name="email" placeholder="[email protected]" required>

<label for="password">Password *</label>
<input type="password" id="password" name="password"
minlength="8" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title="8+ chars, uppercase, lowercase, number" required>
</fieldset>

<fieldset>
<legend>Preferences</legend>

<label for="country">Country</label>
<select id="country" name="country">
<option value="">— Select —</option>
<option value="in">India</option>
<option value="us">United States</option>
</select>

<label>Newsletter</label>
<input type="checkbox" id="news" name="newsletter" value="yes" checked>
<label for="news">Send me updates</label>
</fieldset>

<button type="submit">Create Account</button>
<button type="reset">Clear</button>
</form>

Accessibility Tips

  • Always pair <label> with for attribute matching the <input> id
  • Use <fieldset> and <legend> to group related controls
  • Use aria-describedby to link help text to inputs
  • Use aria-required="true" alongside required for screen readers
  • Error messages should be linked with aria-live="polite" or role="alert"
  • Ensure colour contrast meets WCAG 2.1 AA standards on all inputs