Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML: Build Accessible Forms with Semantic Elements

- 26.07.26 - ErcanOPAK

📝 Accessible Forms = Inclusive Experience

Forms are the primary way users interact. Semantic HTML and ARIA ensure everyone can complete your forms successfully.

📝 Complete Accessible Form

<form 
    action="/submit"
    method="POST"
    novalidate
    aria-labelledby="form-title"
    role="form"
>
    <h2 id="form-title">Contact Us</h2>

    <div class="form-group">
        <label for="name" class="required">
            Full Name
            <span aria-hidden="true">*</span>
        </label>
        <input 
            type="text" 
            id="name" 
            name="name" 
            required
            aria-required="true"
            placeholder="John Doe"
            autocomplete="name"
        />
        <div class="error-message" role="alert" aria-live="polite"></div>
    </div>

    <div class="form-group">
        <label for="email">
            Email Address
            <span aria-hidden="true">*</span>
        </label>
        <input 
            type="email" 
            id="email" 
            name="email" 
            required
            aria-required="true"
            aria-describedby="email-hint"
            autocomplete="email"
        />
        <div id="email-hint" class="hint">
            We'll never share your email
        </div>
        <div class="error-message" role="alert" aria-live="polite"></div>
    </div>

    <div class="form-group" role="radiogroup" aria-labelledby="contact-method-label">
        <fieldset>
            <legend id="contact-method-label">Preferred Contact Method</legend>
            
            <div class="radio-group">
                <input 
                    type="radio" 
                    id="email-contact" 
                    name="contact-method" 
                    value="email"
                    checked
                />
                <label for="email-contact">Email</label>
            </div>
            
            <div class="radio-group">
                <input 
                    type="radio" 
                    id="phone-contact" 
                    name="contact-method" 
                    value="phone"
                />
                <label for="phone-contact">Phone</label>
            </div>
        </fieldset>
    </div>

    <div class="form-group">
        <label for="message" class="required">
            Your Message
            <span aria-hidden="true">*</span>
        </label>
        <textarea 
            id="message" 
            name="message" 
            rows="5"
            required
            aria-required="true"
            aria-describedby="message-count"
        ></textarea>
        <div id="message-count" class="counter" aria-live="polite">
            0 / 500 characters
        </div>
        <div class="error-message" role="alert" aria-live="polite"></div>
    </div>

    <div class="form-group">
        <div class="checkbox-group">
            <input 
                type="checkbox" 
                id="consent" 
                name="consent"
                required
                aria-required="true"
            />
            <label for="consent">
                I agree to the <a href="/terms" target="_blank">terms and conditions</a>
                <span aria-hidden="true">*</span>
            </label>
        </div>
        <div class="error-message" role="alert" aria-live="polite"></div>
    </div>

    <div class="form-actions">
        <button 
            type="submit" 
            class="primary"
            aria-busy="false"
        >Send Message</button>
        <button 
            type="reset" 
            class="secondary"
        >Clear Form</button>
    </div>
</form>

🎯 JavaScript Accessibility

// Live Validation
const form = document.querySelector('form');
const inputs = form.querySelectorAll('input[required], textarea[required]');

inputs.forEach(input => {
    input.addEventListener('blur', validateField);
    input.addEventListener('input', validateField);
});

function validateField(e) {
    const field = e.target;
    const errorElement = field.closest('.form-group')
        .querySelector('.error-message');
    
    if (field.validity.valid) {
        field.setAttribute('aria-invalid', 'false');
        errorElement.textContent = '';
        errorElement.style.display = 'none';
    } else {
        field.setAttribute('aria-invalid', 'true');
        errorElement.textContent = getErrorMessage(field);
        errorElement.style.display = 'block';
        errorElement.setAttribute('role', 'alert');
    }
}

function getErrorMessage(field) {
    if (field.validity.valueMissing) {
        return `Please enter your ${field.name.replace(/-/g, ' ')}`;
    }
    if (field.validity.typeMismatch) {
        return `Please enter a valid ${field.type}`;
    }
    if (field.validity.tooShort) {
        return `Minimum ${field.minLength} characters required`;
    }
    return 'Please correct this field';
}

// Submit Handler
form.addEventListener('submit', function(e) {
    e.preventDefault();
    
    const submitButton = this.querySelector('button[type="submit"]');
    submitButton.setAttribute('aria-busy', 'true');
    submitButton.textContent = 'Sending...';
    
    setTimeout(() => {
        const successMessage = document.getElementById('success-message');
        successMessage.textContent = 'Form submitted successfully!';
        successMessage.setAttribute('role', 'status');
        successMessage.focus();
        
        submitButton.setAttribute('aria-busy', 'false');
        submitButton.textContent = 'Message Sent ✓';
        submitButton.disabled = true;
    }, 2000);
});

💡 Accessible Form Tips

  • Use <label> with for=”id” for all inputs
  • Use required and aria-required=”true”
  • Provide clear error messages with role=”alert”
  • Use autocomplete attributes
  • Ensure focus management

“Accessible forms improve UX for everyone. Good form design leads to better conversion rates and happier users.”

— Accessibility Consultant

Related posts:

HTML5: Fix a label Wrapping a Custom Web Component's Input Failing to Toggle It on Click Because Imp...

HTML5: Fix a label Wrapping a Custom Web Component's Input Failing to Toggle It on Click Because Imp...

HTML5: Building Native Modals with the  Element

HTML5: Building Native Modals with the Element

required Attribute Is NOT Validation

required Attribute Is NOT Validation

Views: 11

Post navigation

CSS: Create Glassmorphism with Advanced Box Shadows
JavaScript: Build Real-time Data Dashboards with Array Methods

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com