Password Field
- Usage
- Styling
Password Field is an input field for entering passwords. The input is masked by default. On mobile devices, though, the last typed letter is shown for a brief moment. The masking can be toggled using an optional reveal button.
new tab
PasswordField passwordField = new PasswordField();
passwordField.setLabel("Password");
passwordField.setValue("Ex@mplePassw0rd");
add(passwordField);
Reveal Button
The reveal button allows the user to disable masking and see the value they entered. This is especially helpful on mobile devices, where typing is more error-prone. In cases where this feature isn’t desired, it can be disabled.
new tab
PasswordField passwordField = new PasswordField();
passwordField.setRevealButtonVisible(false);
passwordField.setLabel("Password");
passwordField.setValue("Ex@mplePassw0rd");
add(passwordField);
Basic Features
The following features, common to most input field components, are supported:
Label
The label is used to identify the input field. It supports plain-text content, and its length is limited to the width of the field. Helpers and Tooltips can be used to provide additional information that doesn’t fit into the label.
Visible labels are strongly recommended for all input fields. In cases where the built-in label cannot be used, an external element can be associated as the field’s label through the aria-labelledby
attribute. Fields without any visible label should include an invisible label for assistive technologies with the aria-label
attribute.
Helper
Helpers are used to provide additional information that the user may need to enter in the field, such as format requirements or explanations of the field’s purpose below the field.
A style variant is available for rendering the helper above the field.
In addition to plain text, helpers can contain components and HTML elements. However, complex and interactive content is likely to have accessibility issues.
Placeholder
The placeholder is text that’s displayed when the field is empty. Its primary purpose is to provide a short input hint (e.g., the expected format) in situations where a Helper cannot be used.
Placeholders should not be used as a replacement for a visible label. They can be mistaken for a manually entered value. See Label for alternatives to the built-in field label.
Tooltip
Tooltips are small text pop-ups displayed on hover, and on keyboard-focus. They can be used to provide additional information about a field. This can be useful in situations where an always visible Helper is not appropriate. Helpers are generally recommended in favor of tooltips, though, as they provide much better discoverability and mobile support. See the Tooltip documentation for more information.
Prefix & Suffix
Prefix and suffix elements — rendered at either end of the field — can be used to display units, icons, and similar visual cues to the field’s purpose or format.
External & Invisible Labels (ARIA)
Visible labels are strongly recommended for all input fields. In situations where the built-in label cannot be used, an external element can be associated as the field’s label through its element id
. Fields without any visible label should be provided an invisible label for assistive technologies like screen readers.
<!-- Associates external element as label: -->
<label id="external-label">This is the label</label>
<vaadin-password-field accessible-name-ref="external-label">...
<!-- Invisible label for screen readers: -->
<vaadin-password-field accessible-name="This is the label">...
new tab
PasswordField field = new PasswordField();
field.setLabel("Label");
field.setHelperText("Helper text");
field.setPlaceholder("Placeholder");
field.setTooltipText("Tooltip text");
field.setClearButtonVisible(true);
field.setPrefixComponent(VaadinIcon.LOCK.create());
Validation
Password Field provides a validation mechanism based on constraints. Constraints allow you to define criteria that the value must meet to be considered valid. Validation occurs typically when the user initiates a value change, for example by entering input and pressing Enter. If the value is invalid, the field is highlighted in red, and an error message appears underneath the input. Certain constraints, however, can proactively prevent users from entering characters that would result in an invalid value.
Below is a list of supported constraints with more detailed information:
Required
Required fields are marked with an indicator next to the label, and become invalid if their value is first entered and then cleared.
An instruction text at the top of the form explaining the required indicator is recommended. The indicator itself can be customized with the --lumo-required-field-indicator
style property.
Min & Max Length
The minimum and maximum input length value constraints dictate the smallest, and the largest number of characters a field accepts. It triggers a validation error if a value shorter than the minimum length is entered, and limits text entered to the maximum length. They can be used to enforce specific formats, or to cap the value to the length supported by the underlying database schema.
In cases where the length requirements may not be clear to the user, it’s recommended to provide this information, for example by using a Helper.
Allowed Characters
A separate single-character, regular expression can be used to restrict the characters that can be entered into the field. Characters that don’t match the expression are rejected. However, values set programmatically are not subject to this restriction, even if they contain disallowed characters.
new tab
PasswordField field = new PasswordField("Password");
field.setRequiredIndicatorVisible(true);
field.setPattern("^[A-Za-z0-9]+$");
field.setMinLength(6);
field.setMaxLength(12);
field.setI18n(new PasswordFieldI18n()
.setRequiredErrorMessage("Field is required")
.setMinLengthErrorMessage("Minimum length is 6 characters")
.setMaxLengthErrorMessage("Maximum length is 12 characters")
.setPatternErrorMessage(
"Only letters A-Z and numbers are allowed"));
It’s important to ensure an appropriate error message is configured for each constraint violation to provide users with clear feedback.
Read-Only & Disabled
Fields used to display values should be set to read-only
mode to prevent editing. Read-only fields are focusable and visible to screen readers. They can display tooltips. Their values can be selected and copied.
Fields that are currently unavailable should be disabled
. The reduced contrast of disabled fields makes them inappropriate for displaying information. They can’t be focused or display tooltips. They’re invisible to screen readers, and their values cannot be selected and copied.
Disabled fields can be useful in situations where they can become enabled based on some user action. Consider hiding fields entirely if there’s nothing the user can do to make them editable.
new tab
PasswordField readonlyField = new PasswordField();
readonlyField.setReadOnly(true);
readonlyField.setLabel("Read-only");
readonlyField.setValue("Ex@mplePassw0rd");
PasswordField disabledField = new PasswordField();
disabledField.setEnabled(false);
disabledField.setLabel("Disabled");
Style Variants
The following style variants can be applied:
Text Alignment
Three different text alignments are supported: left
, which is the default; center
; and right
.
Right-alignment is recommended for numerical values when presented in vertical groups. This tends to aid interpretation and comparison of values.
Small Variant
The small variant can be used to make individual fields more compact. The default size of fields can be customized with style properties.
Helper Above Field
The helper can be rendered above the field, and below the label.
Borders
Borders can be applied to the field surface by providing a value (e.g., 1px
) to the --vaadin-input-field-border-width
CSS property. This can be applied globally to all input fields using the html
selector, or to individual component instances. Borders are required to achieve WCAG 2.1 level AA conformant color contrast with the default Lumo styling of fields.
You can override the default border color with the --vaadin-input-field-border-color
property.
new tab
PasswordField field = new PasswordField();
field.addThemeVariants(TextFieldVariant.LUMO_SMALL,
TextFieldVariant.LUMO_ALIGN_RIGHT,
TextFieldVariant.LUMO_HELPER_ABOVE_FIELD);
field.getStyle().set("--vaadin-input-field-border-width", "1px");
Best Practices
Express clearly your password requirements to the user, so that they don’t have to guess. The Helper feature is appropriate for this purpose.
new tab
PasswordField passwordField = new PasswordField();
passwordField.setLabel("Password");
passwordField.setHelperText(
"A password must be at least 8 characters. It has to have at least one letter and one digit.");
passwordField.setPattern("^(?=.*[0-9])(?=.*[a-zA-Z]).{8}.*$");
passwordField.setErrorMessage("Not a valid password");
add(passwordField);
Showing the strength of the entered password can also be a motivating factor for users to create better passwords. You could display it with a more advanced Helper:
new tab
PasswordField passwordField = new PasswordField();
passwordField.setLabel("Password");
passwordField.setRevealButtonVisible(false);
checkIcon = VaadinIcon.CHECK.create();
checkIcon.setVisible(false);
checkIcon.getStyle().set("color", "var(--lumo-success-color)");
passwordField.setSuffixComponent(checkIcon);
Div passwordStrength = new Div();
passwordStrengthText = new Span();
passwordStrength.add(new Text("Password strength: "),
passwordStrengthText);
passwordField.setHelperComponent(passwordStrength);
add(passwordField);
F578D95F-02B0-43C1-96CD-26926B472701