Required If Web Component Demo

A web component that makes form fields conditionally required based on the values of other fields. See the README for installation and API documentation.

Basic Conditional Requirements

Simple conditions that make a field required when another field has any value.

Field Required if Another Field Has Any Value

Using conditions="email=*" makes the field required when the email field contains any value.

<label>Email address
  <input type="email" name="email" />
</label>

<form-required-if 
  conditions="email=*" 
  indicator="*">
  <label for="phone">Phone number
    <input 
      name="phone" 
      id="phone" 
      type="tel" aria-describedby="phone-required" />
  </label>
  <small id="phone-required">This field becomes required if the email field is filled out</small>
</form-required-if>
This field becomes required if the email field is filled out

OR Logic

Make a field required when any of multiple conditions are met using the || operator.

Multiple Conditions (OR)

Using conditions="email=*||choose=3" makes the field required when either the email field has a value OR the select has "Option 3" chosen.

<label>Email address
  <input type="email" name="email" />
</label>

<label for="choose">Choose an option
  <select name="choose" id="choose">
    <option value="">Select...</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</label>

<form-required-if 
  conditions="email=*||choose=3"
  indicator="*">
  <label for="details">Details
    <input name="details" id="details" aria-describedby="details-required" />
  </label>
  <small id="details-required">Required if email has a value OR Option 3 is selected</small>
</form-required-if>
Required if email has a value OR Option 3 is selected

Specific Value Matching

Require a field only when another field matches an exact value.

Exact Value Match

Using conditions="email=foo@bar.tld" makes the field required only when the email field contains that exact value.

<label>Email address
  <input type="email" name="email" />
</label>

<form-required-if 
  conditions="email=foo@bar.tld">
  <label for="secret">Secret code
    <input name="secret" id="secret" aria-describedby="secret-required" />
  </label>
  <small id="secret-required">Try entering "foo@bar.tld" as the email address</small>
</form-required-if>
Try entering "foo@bar.tld" as the email address

Radio & Checkbox Integration

Common pattern for "Other" options in radio and checkbox groups.

Radio "Other" Pattern

When a user selects "Other", a text field becomes required for them to specify.

<fieldset>
  <legend>Choose an option</legend>
  <label>
    <input type="radio" 
      name="radio" value="1" />
    Option 1
  </label>
  <label>
    <input type="radio" 
      name="radio" value="2" />
    Option 2
  </label>
  <div class="other-radio">
    <label>
      <input type="radio" 
        name="radio" value="other" />
      Other
    </label>
    <form-required-if 
      conditions="radio=other">
      <label class="dependent">
        <span class="visibly-hidden">Other value</span>
        <input name="radio-other" />
      </label>
    </form-required-if>
  </div>
</fieldset>
Choose an option

Checkbox "Other" Pattern

Similar to radio, but works with checkbox arrays using checkbox[]=other syntax.

<fieldset>
  <legend>Choose options</legend>
  <label>
    <input type="checkbox" 
      name="checkbox[]" value="1" />
    Option 1
  </label>
  <label>
    <input type="checkbox" 
      name="checkbox[]" value="2" />
    Option 2
  </label>
  <div class="other-checkbox">
    <label>
      <input type="checkbox" 
        name="checkbox[]" 
        value="other" />
      Other
    </label>
    <form-required-if 
      conditions="checkbox[]=other">
      <label class="dependent">
        <span class="visibly-hidden">Other value</span>
        <input name="checkbox-other" />
      </label>
    </form-required-if>
  </div>
</fieldset>
Choose options

Custom Indicators

Customize the required indicator with HTML and control its position.

HTML Indicator

The indicator attribute accepts HTML, like indicator="<b>*</b>".

<form-required-if 
  conditions="email=*" 
  indicator="<b>*</b>">
  <label for="company">Company
    <input name="company" id="company" aria-describedby="company-required" />
  </label>
  <small id="company-required">Uses a bold asterisk as the indicator</small>
</form-required-if>
Uses a bold asterisk as the indicator

Indicator Positioning

Use indicator-position="before" to place the indicator before the label text instead of after.

<form-required-if 
  conditions="email=*"
  indicator="*"
  indicator-position="before">
  <label for="department">Department
    <input 
      name="department" 
      id="department" aria-describedby="department-required" />
  </label>
  <small id="department-required">Indicator appears before the label text</small>
</form-required-if>
Indicator appears before the label text

Label Structure Variations

The component works with different label / input markup patterns.

Sibling Label & Input

label and input as siblings instead of the input being nested in the label.

<form-required-if 
  conditions="email=*" 
  indicator="*">
  <label for="field1">
    Field label
  </label>
  <input 
    type="text" 
    id="field1" 
    name="field1" />
</form-required-if>

Label with Nested Elements

Works with labels containing one or more nested elements.

<form-required-if 
  conditions="email=*" 
  indicator="*">
  <label for="field2">
    <span>Multi-part</span>
    <span>label text</span>
  </label>
  <input id="field2" name="field2" />
</form-required-if>

Label Wrapping Input

Label can wrap the input element.

<form-required-if 
  conditions="email=*" 
  indicator="*">
  <label>
    <span>Wrapped input</span>
    <input name="field3" />
  </label>
</form-required-if>

Complex Examples

More advanced use cases combining multiple techniques.

Chained Dependencies

Field A makes Field B required, and Field B makes Field C required.

<label>Enable feature
  <input 
    type="checkbox" 
    name="enable" 
    value="yes" />
</label>

<form-required-if 
  conditions="enable=yes">
  <label for="feature-type">
    Feature type
    <select 
      name="feature-type" 
      id="feature-type" aria-describedby="feature-required">
      <option value="">Select...</option>
      <option value="basic">Basic</option>
      <option value="advanced">
        Advanced
      </option>
    </select>
  </label>
  <small id="feature-required">Required when feature is enabled</small>
</form-required-if>

<form-required-if 
  conditions="feature-type=advanced">
  <label for="config">
    Advanced config
    <textarea 
      name="config" 
      id="config" aria-describedby="config-required"></textarea>
  </label>
  <small id="config-required">Required when "Advanced" is selected</small>
</form-required-if>
Required when feature is enabled Required when "Advanced" is selected

Real-World Example

A complete contact form demonstrating practical usage.

Contact Form with Conditional Fields

This form collects contact preferences and conditionally requires contact details.

<form method="dialog">
  <label>Name
    <input type="text" 
      name="name" required />
  </label>

  <fieldset>
    <legend>
      How should we contact you?
    </legend>
    <label>
      <input type="checkbox" 
        name="contact[]" 
        value="email" />
      Email
    </label>
    <label>
      <input type="checkbox" 
        name="contact[]" 
        value="phone" />
      Phone
    </label>
  </fieldset>

  <form-required-if 
    conditions="contact[]=email">
    <label for="contact-email">
      Email address
      <input type="email" 
        name="contact-email" 
        id="contact-email" />
    </label>
  </form-required-if>

  <form-required-if 
    conditions="contact[]=phone">
    <label for="contact-phone">
      Phone number
      <input type="tel" 
        name="contact-phone" 
        id="contact-phone" />
    </label>
  </form-required-if>

  <label for="message">Message
    <textarea name="message" 
      id="message" 
      required></textarea>
  </label>

  <button type="submit">
    Check details
  </button>
</form>
How should we contact you?

Validation only: no message will be sent. Choose Email or Phone, then provide the matching contact details. Name and Message are always required.

API Reference

For complete documentation of attributes, methods, and browser support, see the README.