HTML Input Attributes

HTML Tutorial

HTML Input Attributes

HTML input attributes provide extra functionality, validation, and control over form input fields. They help define behavior, appearance, and restrictions of <input> elements.


Common Input Attributes

AttributeDescriptionExample
typeDefines the input type (text, email, password, etc.)<input type="text">
nameName of the input field (used in form submission)<input name="username">
idUnique identifier (used with labels or JavaScript)<input id="email">
valuePredefined value of the input<input type="text" value="John">
placeholderShows hint text inside input<input placeholder="Enter your name">
requiredMakes input mandatory<input required>
readonlyInput cannot be edited<input readonly value="Fixed text">
disabledInput is disabled<input disabled>
maxlengthMaximum number of characters allowed<input maxlength="10">
min / maxMinimum and maximum value (for number, date, range)<input type="number" min="1" max="100">
stepIncrement value for numeric input<input type="number" step="5">
patternRegular expression for validation<input pattern="[A-Za-z]{3,}">
autocompleteEnable/disable autofill<input autocomplete="off">
sizeWidth of the input field (in characters)<input size="30">
multipleAllow multiple values (email or file)<input type="file" multiple>
autofocusAutomatically focus the input on page load<input autofocus>

Example HTML Form Using Input Attributes


 


Output Preview

  • Username field with placeholder and max length

  • Email field with required validation

  • Age field with min, max, and step values

  • Password field with pattern validation

  • Submit and Reset buttons


Key Points

  • Input attributes enhance form usability and data validation.

  • Attributes like required, pattern, maxlength, min, and max improve form accuracy without JavaScript.

  • Always combine input types and attributes for better user experience and accessibility.

You may also like...