npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@aarongustafson/form-required-checkboxes

v3.2.4

Published

Web component that enables requirement rules for checkbox groups.

Readme

Required Checkboxes Web Component

npm version Build Status

Currently, we can only make checkboxes required or not, individually. In some cases you need to be able to set a specific number of checkboxes that need to be checked. The form-required-checkboxes web component enables that.

TypeScript & Framework Support

This component ships with full TypeScript definitions (.d.ts), enabling type-safe usage in TypeScript projects and improved autocompletion in editors. Types are exported in the package and automatically picked up by modern build tools.

Property Reflection & Framework Compatibility

All key properties (required, notice, error, lang) are now fully reflected as attributes, ensuring compatibility with frameworks and declarative usage. The component also implements an internal _upgradeProperty method for seamless property/attribute sync when used with frameworks that set properties before element definition.

Demos

Installation

npm install @aarongustafson/form-required-checkboxes

Usage

Option 1: Import the class and define manually

Import the class and define the custom element with your preferred tag name:

import { FormRequiredCheckboxesElement } from '@aarongustafson/form-required-checkboxes';

// Define with default name
customElements.define('form-required-checkboxes', FormRequiredCheckboxesElement);

// Or define with a custom name
customElements.define('my-checkbox-group', FormRequiredCheckboxesElement);

Option 2: Auto-define the custom element (browser environments only)

Use the guarded definition helper to register the element when customElements is available:

import '@aarongustafson/form-required-checkboxes/define.js';

If you prefer to control when the element is registered, call the helper directly:

import { defineFormRequiredCheckboxes } from '@aarongustafson/form-required-checkboxes/define.js';

defineFormRequiredCheckboxes();

You can also include the guarded script from HTML:

<script src="./node_modules/@aarongustafson/form-required-checkboxes/define.js" type="module"></script>

CDN Usage

You can also use the component directly from a CDN:

<script src="https://unpkg.com/@aarongustafson/form-required-checkboxes@latest/define.js" type="module"></script>

API

Attributes

Static Methods

Localization

The component includes built-in translations for 16 languages. Messages are automatically generated based on the lang attribute.

Supported languages:

  • en - English
  • zh - Chinese (Mandarin)
  • hi - Hindi
  • es - Spanish
  • fr - French
  • ar - Arabic
  • bn - Bengali
  • pt - Portuguese
  • ru - Russian
  • ja - Japanese
  • de - German
  • pa - Punjabi
  • jv - Javanese
  • ko - Korean
  • vi - Vietnamese
  • it - Italian

Regional language codes (e.g., en-US, es-MX) automatically fall back to their base language.

Using Built-in Languages

<form-required-checkboxes required="3" lang="es">
  <fieldset>
    <legend>Opciones</legend>
    <!-- Will display: "Elija 3 de la lista" -->
  </fieldset>
</form-required-checkboxes>

The component will automatically detect the language from:

  1. The lang attribute on the element itself
  2. The lang attribute on the nearest ancestor element
  3. The document's lang attribute
  4. Falls back to English if none found

Registering Custom Translations

You can register custom translations or override existing ones:

import { FormRequiredCheckboxesElement } from '@aarongustafson/form-required-checkboxes/form-required-checkboxes.js';

FormRequiredCheckboxesElement.registerTranslations({
  pt: {
    exact: "Escolha {n} da lista",
    max: "Escolha até {n} da lista",
    range: "Escolha entre {min} e {max} da lista",
    error_exact: "Você deve escolher exatamente {n} opções",
    error_range: "Você deve escolher entre {min} e {max} opções"
  }
});

Overriding Specific Messages

You can override just specific messages for a language:

FormRequiredCheckboxesElement.registerTranslations({
  en: {
    exact: "Pick exactly {n} items"
    // Other messages will use defaults
  }
});

Per-Instance Overrides

You can still override messages on individual instances using attributes:

<form-required-checkboxes 
  required="3" 
  notice="Select exactly 3 items please"
  error="You must select 3 items">
  <!-- ... -->
</form-required-checkboxes>

Markup Assumptions

This web component assumes you will be marking up your checkbox group in a fieldset with a legend and that all of the checkboxes share a name (e.g., "foo[]").

Examples

Basic Usage - Exact Number Required

<form>
  <form-required-checkboxes required="3">
    <fieldset>
      <legend>Choose your top 3 preferences</legend>
      <label><input type="checkbox" name="preferences[]" value="1"> Option 1</label>
      <label><input type="checkbox" name="preferences[]" value="2"> Option 2</label>
      <label><input type="checkbox" name="preferences[]" value="3"> Option 3</label>
      <label><input type="checkbox" name="preferences[]" value="4"> Option 4</label>
      <label><input type="checkbox" name="preferences[]" value="5"> Option 5</label>
    </fieldset>
  </form-required-checkboxes>
  
  <button type="submit">Submit</button>
</form>

Range of Required Checkboxes

<form-required-checkboxes required="2-4">
  <fieldset>
    <legend>Choose 2-4 items</legend>
    <label><input type="checkbox" name="items[]" value="a"> Item A</label>
    <label><input type="checkbox" name="items[]" value="b"> Item B</label>
    <label><input type="checkbox" name="items[]" value="c"> Item C</label>
    <label><input type="checkbox" name="items[]" value="d"> Item D</label>
    <label><input type="checkbox" name="items[]" value="e"> Item E</label>
  </fieldset>
</form-required-checkboxes>

Optional Maximum (0-N)

<form-required-checkboxes required="0-3">
  <fieldset>
    <legend>Choose up to 3 options (optional)</legend>
    <label><input type="checkbox" name="options[]" value="x"> Option X</label>
    <label><input type="checkbox" name="options[]" value="y"> Option Y</label>
    <label><input type="checkbox" name="options[]" value="z"> Option Z</label>
  </fieldset>
</form-required-checkboxes>

Custom Notice and Error Messages

<form-required-checkboxes 
  required="3" 
  notice="You must select exactly 3 skills" 
  error="Please select exactly 3 skills to continue">
  <fieldset>
    <legend>Select your top 3 skills</legend>
    <label><input type="checkbox" name="skills[]" value="js"> JavaScript</label>
    <label><input type="checkbox" name="skills[]" value="py"> Python</label>
    <label><input type="checkbox" name="skills[]" value="java"> Java</label>
    <label><input type="checkbox" name="skills[]" value="go"> Go</label>
    <label><input type="checkbox" name="skills[]" value="rust"> Rust</label>
  </fieldset>
</form-required-checkboxes>

Browser Support

This web component works in all modern browsers that support:

  • Custom Elements v1
  • ES Modules (for module usage)

For older browsers, you may need polyfills for Custom Elements.

Development

Testing

# Run tests
npm test

# Run tests once
npm run test:run

# Run tests with UI
npm run test:ui

# Run tests with coverage
npm run test:coverage

Linting and Formatting

# Lint code
npm run lint

# Format code
npm run format

Changelog

3.1.0 (Unreleased)

  • TypeScript definitions: Added .d.ts file and package export for full TypeScript support.
  • Property reflection: All key properties (required, notice, error, lang) now reflect as attributes for framework compatibility.
  • _upgradeProperty: Ensures property/attribute sync for frameworks that set properties before element definition.
  • Performance/memory: Uses requestAnimationFrame for initialization, optimizes DOM queries, and ensures all event listeners are cleaned up in disconnectedCallback.
  • Test coverage: Expanded and improved tests for property reflection, upgrade, and event cleanup.