# maxLength

> This document is the Markdown version of [valibot.dev/api/maxLength/](https://valibot.dev/api/maxLength/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Creates a max length validation action.

```ts
const Action = v.maxLength<TInput, TRequirement, TMessage>(
  requirement,
  message
);
```

## Generics

- `TInput` `extends LengthInput`
- `TRequirement` `extends number`
- `TMessage` `extends ErrorMessage<MaxLengthIssue<TInput, TRequirement>> | undefined`

## Parameters

- `requirement` `TRequirement`
- `message` `TMessage`

### Explanation

With `maxLength` you can validate the length of a string or array. For strings, length is measured like JavaScript's `value.length`, i.e. by UTF-16 code units. For arrays, length is measured by the number of items. If the input does not match the `requirement`, you can use `message` to customize the error message.

> If you need to validate the maximum number of user-perceived characters, use [`maxGraphemes`](/api/maxGraphemes.md) instead. If you need to count each Unicode code point as a single character (a supplementary code point occupies two UTF-16 code units in JavaScript), use [`maxCodePoints`](/api/maxCodePoints.md) instead.

## Returns

- `Action` `MaxLengthAction<TInput, TRequirement, TMessage>`

## Examples

The following examples show how `maxLength` can be used.

### Maximum string length

Schema to validate a string with a maximum length of 32 UTF-16 code units.

```ts
const MaxStringSchema = v.pipe(
  v.string(),
  v.maxLength(32, 'The string must not exceed 32 UTF-16 code units.')
);
```

### Maximum array length

Schema to validate an array with a maximum length of 5 items.

```ts
const MaxArraySchema = v.pipe(
  v.array(v.number()),
  v.maxLength(5, 'The array must not exceed 5 numbers.')
);
```

## Related

The following APIs can be combined with `maxLength`.

### Schemas

[`any`](/api/any.md), [`array`](/api/array.md), [`custom`](/api/custom.md), [`instance`](/api/instance.md), [`string`](/api/string.md), [`tuple`](/api/tuple.md), [`unknown`](/api/unknown.md)

### Methods

[`pipe`](/api/pipe.md)

### Actions

[`maxCodePoints`](/api/maxCodePoints.md), [`maxGraphemes`](/api/maxGraphemes.md)

### Utils

[`isOfKind`](/api/isOfKind.md), [`isOfType`](/api/isOfType.md)
