Can validity api provide a means of displaying errors in a list format?

Currently, I'm working on developing a contact form and attempting to utilize the validity API for the first time. Adapting to WCAG2.1 standards has posed some challenges, but I've managed to display errors underneath each field as I code. However, I also need a feature that displays a list of invalid fields at the top of the form when it is submitted.

    
                // JavaScript code snippet goes here...
                

                /* CSS styling rules go here... */
                

                <!-- HTML code snippet goes here... -->
                

Please disregard the CSS for now, as I will work on refining it when integrating with our website.

Answer №1

Consider implementing a div element to contain the error list, positioning it at the top of the form.

You can then easily incorporate some code into your showError() function. In this example, I simply checked the text content of the various error span elements you have already created. If an error is present, it will be displayed on top of the form.

const form  = document.getElementsByTagName('form')[0];
    const email = document.getElementById('mail');
    // More JavaScript code snippets...
body {
    font: 1em sans-serif;
    width: 200px;
    padding: 0;
    margin : 0 auto;
  }
  /* More CSS styling... */
<!DOCTYPE html>
<html>
<head>
    <script src="jquery-3.6.0.min.js"></script>
    <script src="kontaktskjema.js"></script>
    <link rel="stylesheet" href="kontakt.css">
    <meta charset="utf-8">
    <title>Detailed custom validation</title>
</head>
<body>
    <div class="errorsOutput"></div>
    <div class="kontaktskjema">
        <form novalidate>
          <p>
          <label for="navn">
          <span>Navn:</span>
        <input type="text" id="navn" name="navn" required minlength="3">
        <span class="error" aria-live="polite"></span>
      </label>
    </p>    
  </div>
    </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for placing a title in the initial column with the help of v-simple-table from Vuetify.js?

I am interested in using the v-simple-table UI component from Vuetify.js to create a table similar to the one shown below. https://i.sstatic.net/QNdpJ.png After creating the code in codesandbox and previewing the output, I noticed that the title is not a ...

passing parameters via routes

I've been working on passing parameters through the URL using this code, but it doesn't seem to be functioning properly. I suspect that the issue lies in the "get(\users:id)" part, but I'm unsure of the correct way to do it: $.ajax ...

Access the data within a jsonArray using Cypress

I'm dealing with a test.json file that contains a jsonArray [{ "EMAIL": "email_1", "FIRST_NAME": "Daniel" }, [{ "EMAIL": "email_2", "FIRST_NAME": "John" }] ] I'm trying to figure out how to use cypre ...

jquery ajax function that returns an object when successful

Below is a brief example of an AJAX call wrapped in a function. MyNS.GetStrings = function (successCallback, errorCallback) { var url = serverUrl + "/GetStrings"; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: ...

Create a form button that triggers the execution of a Python function

Need help with calling a Python function with a form button Greetings, everyone! I am encountering an issue and would greatly appreciate any assistance. I have a webpage that contains a single button within a Python structure: <form action = "http://l ...

Why are the variables not reflecting the changes when an event is triggered?

I'm a beginner in programming and would really appreciate some guidance. Why are my variables not updating when I click the button?? Here is the HTML code snippet: <h1>NIM</h1> <p>Welcome to a simple edition of the game NIM</p& ...

An error occurred with redirecting using jQuery Mobile's new method in version 1.5

Utilizing jQuery Mobile for its history state feature, I am looking to redirect users to another "page" using the jQuery method recommended in their latest documentation. p/s: I prefer the jQuery navigation method due to the hashchange/history support and ...

Downloading a PDF file on the server is not supported by React

import React from 'react'; import Navigation from "../components/Nagivation"; const Home = () => { return ( <div className="home"> <Navigation/> <div className="homeC ...

Having trouble with the Bootstrap 5 modal in the initial template?

I'm encountering an issue with setting up a basic modal using the Bootstrap 5 starter template paired with the Bootstrap Bundle including Popper. Despite reviewing similar problems on other platforms, none of the solutions seem to resolve my specific ...

Modify the color of the object model when clicked - Three.js

When a user clicks on a specific part of the object model, I want to display the wireframe of that part to indicate which part is being modified. The user can then choose a color for that part from a palette. However, the line child.material.color.set(se ...

How can I make my webpage fill the entire width of an iPhone screen when in landscape mode using Angular or React?

While testing my website on my iPhone, I noticed a significant gap appearing on either side of the app in landscape view in Safari. Is there a solution to fix this issue? The problem occurs in both Angular and React applications, examples of which are disp ...

Enhancing Performance of D3 JS for Visualizing Large XML Data

I am working on visualizing a large XML file, almost 1 GB in size, as a graph of nodes and links using D3 Javascript. I am currently working with mac 10.5.8. I have successfully extracted the content of the file, displaying it as: [Object Element]. The p ...

The res.send() function is being executed prior to the async function being called in express.js

My current project involves creating an API that returns epoch time. I am using an express.js server for this, but the issue arises when the res.send() function is called before the getTimeStamp() function finishes executing. I tried looking up a solution ...

Filter the object by its unique identifier and locate the entry with the highest score

I'm currently working on figuring out the proper syntax for sorting an object by ID and then identifying the highest score within that ID array. While I've managed to sort the object up to this point using conditionals and the sort() method, I&ap ...

FormData causing unexpected behavior with pathInfo()

Feeling a bit lost, is this normal behavior? Javascript - Example 01 function convertCanvasToImage() { var temp_ctx, temp_canvas; temp_canvas = document.createElement('canvas'); te ...

Guide to positioning a logo at the center of a bootstrap navbar

I need help positioning my logo on the navbar without stretching it. How can I make sure it overlaps or doesn't affect the size of the navbar? <nav class="navbar navbar-expand-lg navbar-light bg-light"> <button class="navbar-togg ...

What could be the reason for the Checkbox's value not showing up after making changes?

In my React and Material UI project, I am facing an issue where I want to check all checkboxes in a list by simply checking one checkbox in a parent component. Despite passing down the correct value of the parent checkbox through props, the visual changes ...

Understanding the mechanism of callback function in NodeJS within the context of routes and controllers

Trying to grasp the concept of callbacks and puzzled by the recurring issue TypeError: callback is not a function Here's my router setup: // getPriceRouter.js router.post('/getPrice', function(req, res) { priceController.getPrice(req, ...

Guide for displaying an image exclusively on the front side of a JSON model object using Three Js

Hey there, I've been working on rendering an image onto a model object using a JSON file. While I've successfully rendered the model itself, I'm facing an issue with getting the image to render from the JSON file. var loader1 = new THREE.As ...

What is the best way to initiate WebXR from an iframe in a Next.js environment?

I am currently working on a Next.js app: https://codesandbox.io/s/next-js-forked-6fgnr7?file=/index.tsx I have implemented the functionality for it to open WebXR on Android Chrome when clicking on the AR button located at the bottom left ("in AR betracht ...