> Markdown version of [Text Field](https://vaadin.com/docs/v23/components/text-field). Section index: [llms.txt](https://vaadin.com/docs/v23/components/llms.txt)

# Text Field

Text Field allows the user to input and edit text. Prefix and suffix components, such as icons, are also supported.

**TypeScript** — `text-field-basic.ts`

```html
<vaadin-text-field label="Street Address" value="Ruukinkatu 2" clear-button-visible>
  <vaadin-icon slot="prefix" icon="vaadin:map-marker"></vaadin-icon>
</vaadin-text-field>
```

**Java** — `TextFieldBasic.java`

```java
TextField textField = new TextField();
textField.setLabel("Street Address");
textField.setValue("Ruukinkatu 2");
textField.setClearButtonVisible(true);
textField.setPrefixComponent(VaadinIcon.MAP_MARKER.create());
add(textField);
```

## <a id="common-input-field-features"></a>Common Input Field Features

Text Field includes all shared input field features.

- [See shared input field features](https://vaadin.com/docs/v23/components/input-fields.md)

The features described here for Text Field are also available for [other single-line text input components](#related-components).

## <a id="placeholder"></a>Placeholder

A placeholder is a short description of the expected input value. It’s only shown when the text field is empty.

**TypeScript** — `text-field-placeholder.ts`

```html
<vaadin-text-field placeholder="Search">
  <vaadin-icon slot="prefix" icon="vaadin:search"></vaadin-icon>
</vaadin-text-field>
```

**Java** — `TextFieldPlaceholder.java`

```java
TextField textField = new TextField();
textField.setPlaceholder("Search");
textField.setPrefixComponent(VaadinIcon.SEARCH.create());
add(textField);
```

Placeholders aren’t universal substitutes for labels, as they are only visible while the field is empty. [Helpers](https://vaadin.com/docs/v23/components/input-fields.md#helper) are a better way to provide information that the user may need when filling in the field. Unless marked as required, fields with placeholders also risk being inadvertently skipped when filling in a form.

Search fields are a good example of where placeholders can be used without a label, if they are prefixed or suffixed with a search icon or next to a search button.

Fields with placeholders, but without labels, should also provide an invisible label using the `aria-label` attribute to ensure that screen readers can access it.

## <a id="clear-button"></a>Clear Button

The optional clear button clears the field’s current value. It’s hidden while the field is empty. Clear buttons are useful for search and filter fields, where it’s common for the user to want to clear the field value. However, in data entry forms, they are typically unnecessary.

**TypeScript** — `text-field-clear-button.ts`

```html
<vaadin-text-field value="Value" clear-button-visible></vaadin-text-field>
```

**Java** — `TextFieldClearButton.java`

```java
TextField textField = new TextField();
textField.setClearButtonVisible(true);
textField.setValue("Value");
add(textField);
```

## <a id="text-alignment"></a>Text Alignment

Three different text alignments are supported: left (default), center, and right.

Right-aligned values are recommended for numerical values when presented in vertical groups, as this makes it easier to interpret and compare values.

**TypeScript** — `text-field-text-alignment.ts`

```html
<vaadin-text-field value="value" theme="align-right"></vaadin-text-field>
```

**Java** — `TextFieldTextAlignment.java`

```java
TextField right = new TextField();
right.setValue("value");
right.addThemeVariants(TextFieldVariant.LUMO_ALIGN_RIGHT);
```

> **Note: Numeric fields**
>
> [Number Field](https://vaadin.com/docs/v23/components/number-field.md) is a better choice for certain numeric fields.

## <a id="prefix-and-suffix"></a>Prefix and Suffix

**TypeScript** — `text-field-prefix-suffix.ts`

```html
<vaadin-text-field label="Username" placeholder="username" value="maverick">
  <vaadin-icon slot="prefix" icon="vaadin:user"></vaadin-icon>
</vaadin-text-field>

<vaadin-text-field
  label="Email Address"
  placeholder="name"
  value="michael"
  theme="align-right"
  maxlength="7"
>
  <div slot="suffix">@example.com</div>
</vaadin-text-field>
```

**Java** — `TextFieldPrefixSuffix.java`

```java
TextField username = new TextField();
username.setPrefixComponent(VaadinIcon.USER.create());
username.setLabel("Username");
username.setValue("maverick");
add(username);

TextField email = new TextField();
email.setSuffixComponent(new Div(new Text("@example.com")));
email.setLabel("Email Address");
email.setValue("michael");
email.addThemeVariants(TextFieldVariant.LUMO_ALIGN_RIGHT);
email.setMaxLength(7);
add(email);
```

Prefixes and suffixes can be used to display currency, units, icons, etc. Text or icons are recommended, but other elements can be used as well, if they fit into the field.

Prefixes and suffixes shouldn’t be used as a replacement for labels, except in cases like search fields, preferably combined with a [placeholder](#placeholder).

> **Note: Prefix and suffix are ignored by screen readers**
>
> Any information relevant for assistive technologies needs to be provided as a label and possibly helper text.

## <a id="min-and-max-input-length"></a>Min and Max Input Length

These properties affect the smallest and largest number of characters a field accepts, by limiting text entry to the max length, and triggering a validation error if a shorter value than the min length is entered. They can be used to enforce specific value formats, or to cap the value to the longest length supported by the underlying database schema.

**TypeScript** — `text-field-min-max-input-length.ts`

```html
<vaadin-text-field
  minlength="5"
  maxlength="5"
  label="Zip code"
  style="width: 6em"
></vaadin-text-field>

<vaadin-text-field
  label="Username"
  helper-text="Max 16 characters"
  minlength="1"
  maxlength="16"
></vaadin-text-field>
```

**Java** — `TextFieldMinMaxInputLength.java`

```java
TextField zipCode = new TextField();
zipCode.setMinLength(5);
zipCode.setMaxLength(5);
zipCode.getStyle().set("width", "6em");
zipCode.setLabel("Zip code");
add(zipCode);

TextField username = new TextField();
username.setMaxLength(16);
username.setHelperText("Max 16 characters");
username.setLabel("Username");
add(username);
```

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](https://vaadin.com/docs/v23/components/input-fields.md#helper).

## <a id="pattern"></a>Pattern

A custom validation pattern can be defined (using regular expressions) to validate input.

**TypeScript** — `text-field-pattern.ts`

```html
<vaadin-text-field
  pattern="^[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}$"
  label="Phone number"
  helper-text="Format: +(123)456-7890"
></vaadin-text-field>
```

**Java** — `TextFieldPattern.java`

```java
TextField textField = new TextField();
textField.setPattern(
        "^[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}$");
textField.setHelperText("Format: +(123)456-7890");
textField.setLabel("Phone number");
add(textField);
```

This example invalidates any value that doesn’t consist exclusively of numbers, parenthesis, dashes, and the plus sign. In cases where the format requirements may not be clear to the user, it’s recommended to provide this information, for example by using a [helper](https://vaadin.com/docs/v23/components/input-fields.md#helper).

### <a id="preventing-invalid-input"></a>Preventing Invalid Input (since V23.2)

To further prevent the user from entering invalid values, the characters that can be entered into the field can be restricted using a regular expression.

**TypeScript** — `text-field-allowed-char-pattern.ts`

```html
<vaadin-text-field
  allowed-char-pattern="[\\d\\-+()]"
  label="Phone number"
  helper-text="Format: +(123)456-7890"
></vaadin-text-field>
```

**Java** — `TextFieldAllowedCharPattern.java`

```java
TextField textField = new TextField();
textField.setAllowedCharPattern("[\\d\\-+()]");
textField.setHelperText("Format: +(123)456-7890");
textField.setLabel("Phone number");
add(textField);
```

## <a id="small-variant"></a>Small Variant

The *small* theme variant can be used to make individual fields more compact.

**TypeScript** — `text-field-small-variant.ts`

```html
<vaadin-text-field theme="small" label="Small size" value="Value"></vaadin-text-field>
```

**Java** — `TextFieldSmallVariant.java`

```java
TextField defaultSize = new TextField();
defaultSize.setLabel("Default size");
defaultSize.setValue("Value");
add(defaultSize);

TextField smallSize = new TextField();
smallSize.addThemeVariants(TextFieldVariant.LUMO_SMALL);
smallSize.setLabel("Small size");
smallSize.setValue("Value");
add(smallSize);
```

> **Note: Avoid overuse**
>
> Size variants should be used sparingly. See [Size and Space](https://vaadin.com/docs/v23/styling/lumo/design-tokens/size-space.md) for details on how to change the default text field size.

## <a id="autoselect"></a>Autoselect

When a field is set to `autoselect`, its contents are selected when the field is focused. Use `autoselect` when the user might to want to replace the entire value rather than tweak it.

| Autoselect | Focused with                           | Result                                   |
| ---------- | -------------------------------------- | ---------------------------------------- |
| `ON`       | Pointing device or keyboard navigation | Contents selected                        |
| `OFF`      | Pointing device                        | Cursor placed where clicked              |
| `OFF`      | Keyboard navigation `Tab`              | Cursor at the end of the input value     |
| `OFF`      | Keyboard navigation `Shift`+`Tab`      | Contents selected\[[1](#_footnotedef_1)] |

## <a id="related-components"></a>Related Components

A variety of components are available for different types of input:

| Component                                                                  | Usage Recommendations                                                                        |
| -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| [Text Area](https://vaadin.com/docs/v23/components/text-area.md)           | Free-form multi-line text input, for text longer than can typically fit on a single line.    |
| [Email Field](https://vaadin.com/docs/v23/components/email-field.md)       | For email addresses.                                                                         |
| [Number Field](https://vaadin.com/docs/v23/components/number-field.md)     | Allows only numeric input.                                                                   |
| [Password Field](https://vaadin.com/docs/v23/components/password-field.md) | For securely entering passwords.                                                             |
| [Combo Box](https://vaadin.com/docs/v23/components/combo-box.md)           | For selecting from a predefined set of options. Allows filtering and entering custom values. |
| [Date Picker](https://vaadin.com/docs/v23/components/date-picker.md)       | Input field for entering or selecting a specific date.                                       |
| [Time Picker](https://vaadin.com/docs/v23/components/time-picker.md)       | Input field for entering or selecting a specific time.                                       |

`E6D6D93F-BDB7-425A-AE47-343981004746`

---

[1](#_footnoteref_1). Consequent keyboard navigation results in the contents being selected until the selection is changed, by either arrow navigation or mouse click.
