The many characteristics for the HTML <input>Â (HTML Input Attributes) element are covered in this chapter.
The value Attribute
An input field’s starting valueis specified by the input value attribute:
Input fields with initial (default) values:
scheme://prefix.domain:port/path/filename
The readonly Attribute
An input field’s read-only status is indicated by the input readonly attribute.
A read-only input field can be accessed, highlighted, and its contents copied, but it cannot be changed.
When the form is submitted, the value of a read-only input field will be communicated!
Example
A read-only input field:
The disabled Attribute
An input field should be disabledaccording to the input disabled attribute.
An input field that is disabled cannot be clicked or used.
When the form is submitted, the value of an input field that is disabled won’t be delivered!
Example
A disabled input field:
The size Attribute
The visible width of an input field, expressed in characters, is specified by the input sizeattribute.
Sizehas a default value of 20.
Note: Text, search, tel, url, email, and password input types are all compatible with the sizeattribute.
Example
Set a width for an input field:
The maxlength Attribute
The maximum character count that can be entered into an input field is indicated by the input maxlengthattribute.
Note: If you select a maxlength, the input field will only take the number of characters that you have provided. Nevertheless, this characteristic doesn’t offer any feedback. Thus, you need to create JavaScript code if you wish to inform the user.
Example
Set a maximum length for an input field:
The min and max Attributes
The minimumand maximumvalues for an input field are specified via the input min and max properties.
The following input types are compatible with the minand maxattributes: number, range, date, datetime-local, month, time, and week.
Advice: To establish a range of permissible values, combine the max and min properties.
Example
Set a max date, a min date, and a range of legal values:
The multiple Attribute
The ability to enter multiplevalues in an input field is indicated by the input multiple property.
Email and file input types are supported by the multipleattribute.
Example
A file upload field that accepts multiple values:
The pattern Attribute
When the form is submitted, the value entered in the input field is compared to the regular expression specified by the input patternattribute.
The input types that the patternattribute accepts are text, date, search, url, tel, email, and password.
Advice: To assist the user, utilize the global title attribute to explain the pattern.
Advice: Take a look at our JavaScript lesson to learn more about regular expressions.
Example
An input field that can contain only three letters (no numbers or special characters):
The placeholder Attribute
A brief suggestion (a sample value or a brief explanation of the expected format) describing the expected value of an input field is specified by the input placeholderattribute.
Before the user inputs a value, a brief hint is shown in the input area.
The input types that the placeholderproperty accepts include text, search, url, phone, email, and password.
Example
An input field with a placeholder text:
The required Attribute
An input field needs to be filled up before the form can be submitted, according to the input needed attribute.
The input types that the needed attribute accepts include text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.
Example
A required input field:
The step Attribute
The permitted number intervals for an input field are specified by the input stepproperty.
Using step=”3″ as an example, the permissible numbers are -3, 0, 3, 6, and so on.
Advice: To define a range of permissible values, combine this attribute with the max and min attributes.
The following input types are compatible with the stepattribute: number, range, date, datetime-local, month, time, and week.
Example
An input field with a specified legal number intervals:
The autofocus Attribute
An input field is set to automatically focus upon page load by using the input autofocusattribute.
Example
Let the “First name” input field automatically get focus when the page loads:
The height and width Attributes
The height and width of a <input type=”image”> element are specified via the input heightand widthproperties.
Example
Define an image as the submit button, with height and width attributes:
The list Attribute
A <datalist> element with pre-defined options for a <input> element is referred to by the input listproperty.
Example
An <input> element with pre-defined values in a <datalist>:
The autocomplete Attribute
Whether autocomplete should be enabled or disabled for a form or input field is specified by the input autocompleteattribute.
The browser is able to predict the value thanks to autocomplete. The browser should offer alternatives to fill in a field based on previously typed data when the user begins to input in it.
The <form>and the <input> types text, search, url, tel, email, password, datepickers, range, and color are compatible with the autocompleteproperty.
Example
An HTML form with autocomplete on, and off for one input field: