-
Hajipur, Bihar, 844101
HTML input attributes allow you to control how form fields behave. These attributes help you set rules, give hints, limit user entries and improve the overall experience. In this chapter, you will learn the most important input attributes with practical examples so you can create better and more accurate forms.
Input attributes are properties added inside an <input> tag. They guide the browser on how the field should work. Some attributes set limits, some provide helpful text and others improve accessibility. You can combine many attributes in a single input.
The value attribute sets the default value of a field. It appears inside the input when the page loads.
<input type="text" value="India">
The placeholder provides a hint or sample text. It disappears as soon as the user starts typing.
<input type="text" placeholder="Enter your full name">
If an input is required, the form cannot submit until the user provides a value.
<input type="email" required>
readonly makes the input uneditable, but the value inside it can still be submitted.
<input type="text" value="Read-only text" readonly>
disabled completely blocks the field. It cannot be edited, selected or submitted.
<input type="text" value="Disabled" disabled>
These attributes control the number of characters a user can type.
<input type="text" minlength="3" maxlength="20">
min and max set the allowed range for number inputs, date inputs and range inputs.
<input type="number" min="1" max="100">
step controls the interval between acceptable values. It works with numbers, dates and ranges.
<input type="number" min="0" max="10" step="2">
The pattern attribute allows you to use regular expressions to validate the input. This gives you more control over formats.
<input type="text" pattern="[0-9]{10}" placeholder="Enter phone number">
autocomplete tells the browser whether to suggest previously entered values.
on
off
<input type="email" autocomplete="off">
autofocus automatically places the cursor inside a field when the page loads.
<input type="text" autofocus>
multiple lets users select more than one value. It is usually used with file and email inputs.
<input type="file" multiple>
size controls the visible width of an input box in characters.
<input type="text" size="40">
The list attribute connects an input field to a <datalist>, giving dropdown suggestions.
<input type="text" list="countries">
<datalist id="countries">
<option value="India">
<option value="Nepal">
<option value="Bhutan">
</datalist>
The name attribute is important for backend processing. Without it, form data will not be submitted with a key.
<input type="text" name="username">
The form attribute links an input to a specific form even if the input is placed outside the form tag.
<form id="f1">
<input type="submit" value="Submit Form">
</form>
<input type="text" form="f1" name="extra">
Used with the submit button. It skips the validation rules in the form.
<input type="submit" formnovalidate>
When used with a submit button, formaction changes the URL where the form data will be sent.
<input type="submit" formaction="/save-details">
Sets the encoding type for form data. Useful when uploading files.
<input type="submit" formenctype="multipart/form-data">
Defines the HTTP method for form submission.
get
post
<input type="submit" formmethod="post">
Opens the form result in a specific location, such as a new tab.
<input type="submit" formtarget="_blank">
<form>
<input type="text" name="fullname" placeholder="Full Name" required minlength="3">
<input type="email" name="email" placeholder="Email" autocomplete="off">
<input type="number" name="age" min="18" max="60" step="1" required>
<input type="file" name="resume" multiple>
<input type="text" list="citylist" name="city" placeholder="Select City">
<datalist id="citylist">
<option value="Delhi">
<option value="Mumbai">
<option value="Chennai">
<option value="Kolkata">
</datalist>
<input type="submit" value="Submit">
</form>
Input attributes play a major role in improving form usability. You learned how attributes like placeholder, required, readonly, disabled, minlength, maxlength, pattern, autocomplete, autofocus, multiple, list, name and many others work. Each attribute controls how users interact with inputs and ensures your form collects clean, structured and accurate data. With proper use of these attributes, your forms become easier to use and more reliable.
Q1. Use required on a username and password field.
Q2. Create an email field that is readonly.
Q3. Add a placeholder text to a search box.
Q4. Limit a text field to 25 characters using maxlength.
Q5. Set a number input between 10 to 100 using min and max.
Q6. Use pattern="[A-Za-z]{3,}" to accept only alphabetic text (min 3 chars).
Q7. Add a file input that allows selecting multiple files.
Q8. Use autofocus on the first input in the form.
Q9. Create a checkbox that is pre-selected using checked.
Q10. Add a password field with pattern=".{8,}" and a title explaining the rule.