html

input form attributes in HTML

The input element is the main stakeholder in building an HTML form. The input element contains a variety of attributes to fully exercise the element. The attributes of the input element enable you to add various features to HTML forms such as a radio button, checkbox, buttons, text fields, and so on. Inspired by the importance of attributes, we have compiled a detailed guide that covers all the attributes supported by the input element.

How input attributes works in HTML

The functionality of the attributes depends upon the values carried by these attributes. The attributes and their supported values are described here.

Type attribute

Like the input element is the core part of HTML forms, the type attribute is the key ingredient of the input element. It defines the input type which has one of the following values.

  • text: used to describe a text field
  • number: used to create a field that accepts numeric values
  • email: used to enter an email address
  • color: define the color of an input field
  • checkbox: to create a checkbox
  • radio: to create a radio button
  • tel: creates a field that accepts telephone numbers
  • image: used to get the image file from the user
  • hidden: a field that will not be displayed on the user’s screen
  • date: creates a field that accepts date
  • month: creates a field that contains month and year to be selected
  • submit: a button that would submit the form data
  • reset: this creates a button that resets all data of form

The above-mentioned are the most used values of the type attribute and the below-stated example makes use of few of them.

Example

The code written below executes a few values of the type attribute.

<form>
  <input type="text"> <br>
  <input type="email"> <br>
  <input type="submit" value="Submit">
  <input type="reset" value="Reset">
</form>

Four different input types are used in the above code that uses the values text, email, submit and reset.

Output

Value attribute

Whenever an HTML form is defined, it is recommended to assign an initial value to each for element. To do so, you have to use value attribute. The input type must be defined to use this attribute.

Example
The code provided below practices value attribute on input type=text and input type=submit.

<form>
  <input type="text" value="msg"><br>
  <input type="submit" value="Submit">
</form>

Output

Read-only attribute

This attribute would not allow writing inside the input field.

Example
The HTML code provided practices the readonly attribute on an input field.

<form>
  <input type="text" readonly><br>
  <input type="submit" value="Submit">
</form>

Output

The text-field shown in the output will neither allow you to write nor the mouse click would take any action.

Disabled attribute

The input type that carries this attribute would display only the area covered by the input field. However, you cannot perform any action on that input element.

Example
The disabled attribute of input element is practiced in the following code

<form>
  <input type="text" disabled><br>
  <input type="submit" value="Submit">
</form>

Output

Note: The read-only attribute is submitted with the form whereas the disabled attribute does permit the value for submission.

Pattern attribute

This attribute accepts regular expressions for which the value of that field will be checked. It is used with few input types such as text, email, tel, url, password, and date.

Example
In the following code, the pattern attribute is used on a text field that will accept only small alphabets.

<form>
  <input type="text" pattern="[a-z]"><br>
  <input type="submit" value="Submit">
</form>

Output

For verification of the pattern attribute, we tried to enter the capital alphabets. However, an error prompted asking to match the requested format.

Placeholder attribute

This attribute adds dummy characters that assist the user in understating the purpose of that field.

Example
The code mentioned below applies the placeholder attribute field to describe that the text-field is meant to accept your name.

<form>
  <input type="text" placeholder="Enter Your Name"><br>
  <input type="submit" value="Submit">
</form>

Output

It is observed from the output that the text-field directs the user to enter their name.

Required attribute

The input type’s that uses required attributes are mandatory to be filled.

Example
The code written below sets the required attribute for a text-field.

<form>
  <input type="text" required><br>
  <input type="submit" value="Submit">
</form>

Output

As the field is mandatory to be filled thus the submission will not be performed until some text is written inside the text-field.

Autofocus attribute

If the web page is reloaded, the input field will be focused in which the autofocus attribute is used.

Example
The following code sets the autofocus attribute of input element.

<form>
  <input type="text" autofocus><br>
  <input type="submit" value="Submit">
</form>

Output

As we have used the autofocus attribute on a text field, thus whenever the page is loaded, the cursor will keep on ticking.

Multiple attribute

This attribute allows adding multiple values (attaching multiple files is the main use of this attribute).

Example
The code provided below takes the file as an input type and multiple attribute is applied on the input type.

<form>
  <input type="file" multiple><br>
  <input type="submit" value="Submit">
</form>

Output

The output shows that three files are attached which describe the working of multiple attributes.

Height and width attribute

This attribute is used with image type and sets the height and width of the image.

Example
The input type is set to image and the height/width attributes are applied on it.

<body>
<form>
  <input type="image" src="file:///C:/Users/adnan/Downloads/index.jpg" height="50" width="50"><br>
  <input type="submit" value="Submit">
</form>

Output

The image in the output would be adjusted according to the value of width and height.

Min/Max attribute

The minimum/maximum values for various input types are specified by these attributes. The input types may include text, number, date, range, month, time, and week.

Example
The code provided below uses the min and max attribute where the input type is set to number.

<form>
  <input type="number" min="2" max="20"><br>
  <input type="submit" value="Submit">
</form>

Output

As shown in the output, the number field is not accepting 21 as the limit was set to 20.

Maxlength attribute

The number of characters for an input type can be specified by using maxlength attribute.

Example
The following code limits the number of characters to 5 in a text field.

<form>
  <input type="text" maxlength="5"><br>
  <input type="submit" value="Submit">
</form>

Output

The output above contains 5 characters in the text field, the text-field will not allow the 6th character due to maxlength attribute.

Conclusion

The input element has an extensive list of attributes to create a featureful HTML form. This informative post lists down the attributes of the input element of HTML forms. The attributes supported by the input element are, type, value, disabled, read-only, autofocus, multiple, height and width, maxlength etc,. Moreover, each attribute is practiced using an example that will help in understating the functionality.

About the author

Adnan Shabbir