# Bad value “” for attribute “name” on element “form”: Must not be empty.

> Canonical HTML version: https://rocketvalidator.com/html-validation/bad-value-for-attribute-name-on-element-form-must-not-be-empty
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The HTML specification requires that if the `name` attribute is used on a `<form>` element, its value must be a non-empty string. An empty `name=""` attribute serves no practical purpose — it doesn't register the form in the `document.forms` named collection, and it can't be used as a valid reference in scripts. The W3C validator flags this as an error because it violates the content model defined in the WHATWG HTML Living Standard.

The `name` attribute on a form is primarily used to access the form programmatically through `document.forms["formName"]`. When the value is empty, this lookup mechanism doesn't work, so the attribute becomes meaningless. This is different from the `id` attribute, which also identifies elements but participates in fragment navigation and CSS targeting. The `name` attribute on `<form>` is specifically for the legacy `document.forms` named getter interface.

It's worth noting that the `name` attribute value on a form must not equal an empty string, and it should be unique among the form elements in the forms collection. While duplicate names won't cause a validation error, they can lead to unexpected behavior when accessing forms by name in JavaScript.

## How to fix

You have two options:

1. **Remove the attribute entirely.** If you're not referencing the form by name in JavaScript, the `name` attribute is unnecessary. You can use `id` instead for CSS or JavaScript targeting.
2. **Provide a meaningful, non-empty value.** If you need to reference the form through `document.forms`, give it a descriptive name that reflects its purpose.

## Examples

### ❌ Invalid: empty `name` attribute

```html
<form name="">
  <label for="email">Email</label>
  <input type="email" id="email" name="email">
  <button type="submit">Subscribe</button>
</form>
```

This triggers the validator error because the `name` attribute is present but has an empty string value.

### ✅ Fixed: attribute removed

```html
<form>
  <label for="email">Email</label>
  <input type="email" id="email" name="email">
  <button type="submit">Subscribe</button>
</form>
```

If you don't need to reference the form by name, simply remove the attribute.

### ✅ Fixed: meaningful name provided

```html
<form name="subscriptionForm">
  <label for="email">Email</label>
  <input type="email" id="email" name="email">
  <button type="submit">Subscribe</button>
</form>
```

With a non-empty name, you can now access the form in JavaScript using `document.forms["subscriptionForm"]` or `document.forms.subscriptionForm`.

### ✅ Alternative: using `id` instead

```html
<form id="subscription-form">
  <label for="email">Email</label>
  <input type="email" id="email" name="email">
  <button type="submit">Subscribe</button>
</form>
```

In modern development, using `id` with `document.getElementById()` or `document.querySelector()` is often preferred over the `name` attribute for form identification. The `name` attribute on `<form>` is a legacy feature that remains valid but isn't required for most use cases.
