HTML input attributes are properties that control the behavior, appearance, and validation of the <input> element in HTML forms.
Here are some common HTML input attributes:
| Attribute | Description | Example |
|---|---|---|
type | Specifies the type of input. | <input type="text"> |
name | Identifies the input when submitting a form. | <input name="username"> |
value | Sets the initial value. | <input value="John"> |
placeholder | Displays hint text. | <input placeholder="Enter your name"> |
required | Makes the field mandatory. | <input required> |
readonly | Prevents editing of the input. | <input readonly> |
disabled | Disables the input field. | <input disabled> |
maxlength | Sets the maximum number of characters. | <input maxlength="20"> |
minlength | Sets the minimum number of characters. | <input minlength="5"> |
min | Specifies the minimum value (for number/date inputs). | <input type="number" min="1"> |
max | Specifies the maximum value. | <input type="number" max="100"> |
step | Specifies valid intervals for numeric values. | <input type="number" step="5"> |
checked | Pre-selects a checkbox or radio button. | <input type="checkbox" checked> |
multiple | Allows multiple file or email selections. | <input type="file" multiple> |
accept | Restricts file types for file uploads. | <input type="file" accept=".jpg,.png"> |
pattern | Specifies a regular expression for validation. | <input pattern="[A-Za-z]{3,}"> |
autocomplete | Enables or disables autocomplete. | <input autocomplete="off"> |
autofocus | Automatically focuses the input on page load. | <input autofocus> |
size | Sets the visible width of the input. | <input size="30"> |
Example
<form> <label>Name:</label> <input type="text" name="username" placeholder="Enter your name" required maxlength="30" autofocus > <br><br> <label>Age:</label> <input type="number" min="1" max="120" required > <br><br> <input type="submit" value="Submit"> </form>
These attributes help define how users interact with form fields, enforce input rules, and improve the overall user experience.
No comments:
Post a Comment