# Bad value “” for attribute “maxlength” on element “input”: The empty string is not a valid non-negative integer.

> Canonical HTML version: https://rocketvalidator.com/html-validation/bad-value-for-attribute-maxlength-on-element-input-the-empty-string-is-not-a-valid-non-negative-integer
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The HTML specification defines `maxlength` as accepting only a **valid non-negative integer** — a string of one or more ASCII digits representing a number greater than or equal to zero. An empty string does not satisfy this requirement, so the W3C validator flags it as an error. This commonly happens when a value is dynamically generated by a CMS, template engine, or JavaScript framework and the value ends up blank, or when a developer adds the attribute as a placeholder intending to fill it in later.

## Why this matters

While most browsers silently ignore an empty `maxlength` and impose no character limit, relying on this behavior is problematic for several reasons:

- **Standards compliance:** Invalid HTML can lead to unpredictable behavior across different browsers and versions. What works today may not work tomorrow.
- **Accessibility:** Assistive technologies may read or interpret the `maxlength` attribute to communicate input constraints to users. An empty value could cause confusing or incorrect announcements.
- **Maintainability:** An empty `maxlength` is ambiguous — it's unclear whether the developer intended no limit, forgot to set a value, or a bug caused the value to be missing.

## How to fix it

You have two options:

1. **Set a valid non-negative integer:** Provide a concrete number that represents the maximum number of characters the user can enter, such as `maxlength="100"`.
2. **Remove the attribute:** If you don't need to enforce a character limit, simply omit `maxlength` altogether. There is no need to include it with an empty value.

Valid values for `maxlength` include `"0"`, `"1"`, `"255"`, or any other non-negative whole number. The following are **not** valid: empty strings (`""`), negative numbers (`"-1"`), decimal numbers (`"10.5"`), or non-numeric strings (`"none"`).

## Where `maxlength` applies

The `maxlength` attribute is meaningful on text-entry `input` types: `text`, `search`, `url`, `tel`, `email`, `password`, and also on the `textarea` element. For non-text input types like `number`, `date`, `range`, or `checkbox`, the attribute has no effect and should not be used.

## Examples

### ❌ Incorrect: empty string triggers the validation error

```html
<input type="text" name="username" maxlength="">
```

### ❌ Incorrect: other invalid values

```html
<input type="text" name="username" maxlength="-1">
<input type="email" name="email" maxlength="none">
<input type="text" name="bio" maxlength="10.5">
```

### ✅ Correct: explicit maximum length

```html
<input type="text" name="username" maxlength="30">
```

### ✅ Correct: omit the attribute when no limit is needed

```html
<input type="text" name="comment">
```

### ✅ Correct: `maxlength` on a `textarea`

```html
<textarea name="bio" maxlength="500"></textarea>
```

### ✅ Correct: dynamic value with a fallback

If your `maxlength` value comes from a template or CMS, make sure you either output a valid number or omit the attribute entirely. For example, in a templating language, use conditional logic:

```html
<!-- Only render maxlength if the value is set -->
<input type="text" name="username" maxlength="100">
```

Rather than rendering an empty attribute like `maxlength=""`, ensure your template skips the attribute when no value is configured.
