> For the complete documentation index, see [llms.txt](https://docs.forms.md/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.forms.md/input-types/text-input.md).

# Text input

<figure><img src="/files/aoHXNkFWtmXwfK32eyVZ" alt=""><figcaption><p>Text input</p></figcaption></figure>

Use the `textInput()` function to create text inputs in your forms. It can be used for single-line or multi-line text inputs. The multi-line ones use the `<textarea>` element instead of the regular `<input type="text">`.

***

## Basic usage

```javascript
import { Composer } from "formsmd";

const composer = new Composer({
  id: "my-form"
});

composer.textInput("fullName", {
  question: "What is your name?"
});
```

Generates the following Markdown-like syntax:

```
#! id = my-form

fullName = TextInput(
  | question = What is your name?
)
```

### Required

Add the `required` parameter to make the field mandatory:

```javascript
composer.textInput("fullName", {
  question: "What is your name?",
  required: true
});
```

Generates the following Markdown-like syntax:

```
fullName* = TextInput(
  | question = What is your name?
)
```

***

## Function overview

The following is the overview of the function:

```typescript
textInput(name: string, params: object)
```

### Arguments

| Name     | Type     | Description                                                                                                                                                     |
| -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`   | `string` | A unique name for your form field that you'll use to identify the user's response.<mark style="color:red;">\*</mark>                                            |
| `params` | `object` | An object containing all the configuration parameters for your text input field (see the [parameters section](#parameters) below for the full list of options). |

{% hint style="danger" %} <mark style="color:red;">\*</mark>Avoid values for the `name` argument which may be the names of HTML attributes, such as `"name"`, `"role"`, `"id"`, etc. This is because by default, the form's template string is first sanitized using [DOMPurify](https://github.com/cure53/DOMPurify), and these values may be removed to prevent DOM clobbering.
{% endhint %}

***

## Parameters

### Shared parameters

These parameters are common to all form fields:

| Name                  | Type                                            | Description                                                                                                                                                                                                                                                                             |
| --------------------- | ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `question` (required) | `string`                                        | The main question or label of the form field.                                                                                                                                                                                                                                           |
| `required`            | `true` (`boolean`)                              | When set, the field becomes required.                                                                                                                                                                                                                                                   |
| `description`         | `string`                                        | Any extra information that the user may need to fill out the form. Appears right below the question.                                                                                                                                                                                    |
| `fieldSize`           | `"sm"`                                          | When set to `"sm"`, the font sizes of the question, description, and answer are made smaller.                                                                                                                                                                                           |
| `labelStyle`          | `"classic"`                                     | When set to `"classic"`, the question and description of the form field are made smaller.                                                                                                                                                                                               |
| `subfield`            | `true` (`boolean`)                              | When set, the question and description of the form field are made smaller. Functionally the same as setting `labelStyle` to `"classic"`.                                                                                                                                                |
| `disabled`            | `true` (`boolean`)                              | When set, the input is disabled.                                                                                                                                                                                                                                                        |
| `autofocus`           | `true` (`boolean`)                              | When set, the input will be automatically focused when the parent slide becomes active, or immediately after page load.                                                                                                                                                                 |
| `id`                  | `string`                                        | The `id` attribute of the form field container.                                                                                                                                                                                                                                         |
| `classNames`          | `string[]`                                      | The CSS class names of the form field. [See the available CSS utility classes](/content/css-utility-classes.md).                                                                                                                                                                        |
| `attrs`               | `Array<{ name: string, value: string }>`        | Other HTML attributes of the form field. Each attribute has a `name` and `value` property.                                                                                                                                                                                              |
| `displayCondition`    | `{ dependencies: string[], condition: string }` | Controls when the field is shown. The `dependencies` lists the fields to watch, and `condition` is the expression that must be true to show the field. The `condition` must be a valid [Nunjucks](https://mozilla.github.io/nunjucks/) expression. [See example](#conditional-display). |

### Text input specific parameters

| Name          | Type               | Description                                                                                         |
| ------------- | ------------------ | --------------------------------------------------------------------------------------------------- |
| `placeholder` | `string`           | Sets the `placeholder` attribute of the input.                                                      |
| `multiline`   | `true` (`boolean`) | When set, the input accepts values with one or more lines because the `<textarea>` element is used. |
| `maxlength`   | `number`           | If set, this becomes the maximum number of allowed characters in the input.                         |
| `pattern`     | `string`           | If set, the input value must match the given pattern.                                               |
| `value`       | `string`           | If set, this becomes the default value of the input.                                                |

***

## Examples

### Multi-line input with character limit

```javascript
composer.textInput("comments", {
  question: "Please share your feedback",
  description: "Your feedback helps us improve our service",
  multiline: true,
  maxlength: 500,
  placeholder: "Type your feedback here..."
});
```

Generates the following Markdown-like syntax:

```
comments = TextInput(
  | question = Please share your feedback
  | description = Your feedback helps us improve our service
  | multiline
  | maxlength = 500
  | placeholder = Type your feedback here...
)
```

### Input with pattern validation

```javascript
composer.textInput("username", {
  question: "Choose a username",
  description: "Letters and numbers only, 3-20 characters",
  pattern: "^[a-zA-Z0-9]{3,20}$",
  required: true
});
```

Generates the following Markdown-like syntax:

```
username* = TextInput(
  | question = Choose a username
  | description = Letters and numbers only, 3-20 characters
  | pattern = ^[a-zA-Z0-9]{3,20}$
)
```

### Styled input with custom attributes

Add CSS classes and other HTML attributes using the `classNames` and `attrs` parameters. Please note, these class names and attributes are added to the `<div>` or `<fieldset>` container that contains the actual input field(s).

```javascript
composer.textInput("fullName", {
  question: "What is your full name?",
  classNames: ["col-6", "xs:col-6"],
  attrs: [
    { name: "style", value: "font-size: 18px;" }
  ]
});
```

Generates the following Markdown-like syntax:

```
[.col-6 .xs:col-6 style="font-size: 18px;"]
fullName = TextInput(
  | question = What is your full name?
)
```

Please [see the available CSS utility classes](/content/css-utility-classes.md).

### Conditional display

Conditionally show or hide an input field using the `displayCondition` parameter. It works as follows:

* `dependencies` lists the fields to watch.
* `condition` is the expression that must be true to show the field. This must be a valid [Nunjucks](https://mozilla.github.io/nunjucks/) expression.

For instance, in the example below, the `otherReason` text input will only show up if the user picks the `"Other"` option in the first choice input.

```javascript
composer.choiceInput("reason", {
  question: "What is your reason?",
  required: true,
  choices: ["A", "B", "C", "Other"]
})

composer.textInput("otherReason", {
  question: "Please specify other reason",
  displayCondition: {
    dependencies: ["reason"],
    condition: "reason == 'Other'"
  }
});
```

Generates the following Markdown-like syntax:

```
reason* = ChoiceInput(
  | question = What is your reason?
  | choices = A, B, C, Other
)

::: [{$ reason $}]
{% if reason == "Other" %}
otherReason = TextInput(
  | question = Please specify other reason
)
{% endif %}
:::
```
