Input fields
Bootstrap 5 Input fields
Input fields refer specifically to the text input fields, which are used to obtain data from the users.
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic example
A basic example of the input field consists of the input
element with specified
ID
and label
element connected via this ID
with the
input. Both elements are wrapped in .form-outline
class which provides a material
design look.
Sizing
Set heights using classes like .form-control-lg
and
.form-control-sm
.
Disabled
Add the disabled
boolean attribute on an input to give it a grayed out appearance
and remove pointer events.
Readonly
Add the readonly
boolean attribute on an input to prevent modification of the input's value.
Types
Input types let you specified what data users should provide and help you validate it.
Text
The input type="text"
defines a single-line text field.
The input field type="email"
defines the email address field. The input value is automatically validated to ensure that it is a properly formatted email address.
Password
The input field type="password"
defines a password field, thus hiding characters as confidential information.
Number
The input type="number"
defines field for entering a number.
Phone number
The input type="tel"
defines a field for entering a telephone number.
URL
The input type="url"
defines a field for entering a URL.
Textarea
The textarea
tag defines a multi-line text input control.
Text
Block-level or inline-level form text can be created using .form-text
.
Associating form text with form controls
Form text should be explicitly associated with the form control it relates to using the
aria-describedby
attribute. This will ensure that assistive technologies—such as
screen readers—will announce this form text when the user focuses or enters the control.
Form text below inputs can be styled with .form-text
. If a block-level element
will be used, a top margin is added for easy spacing from the inputs above.
Inline text can use any typical inline HTML element (be it a <span>
,
<small>
, or something else) with nothing more than the
.form-text
class.
Helper text
Add class form-helper
to div
to create helper text.
Character counter
Add an empty form-helper
element, set
data-mdb-show-counter="true"
and set the maxlength
attribute to
create a counter.
Icons
Trailing icon
Add class trailing
to i
tag to create trailing icon in the input.
Password Visibility Toggle
Check the box or click the icon to reveal the password, allowing users to verify their input without retyping.
Updating label width
If any form parent is hidden, after displaying it, update the input to recalculate width of the label using the following code:
Dynamic input initialization
If the input is dynamically loaded on the page, then after displaying it, the initialization should be performed as in the example below.