Skip to content
Advertisement

Loop through all inner elements of HTML and check if field is required

I’m working on a complex multipage form using HTML, Bootstrap and JavaScript.

What I’m trying to figure out is to validate each section of the multipage form and check if all required fields (if any) are actually valid so that the user can proceed to next section in the form.

The following code snippet is a simplification of my modified form:

JavaScript

What is the most efficient way of getting all required fields? For now I’m iterating through each item in the section-1 and get the inner parts to check validity. But using my code below, it will not work on complex nested nodes. How to prevent that?

JavaScript

enter image description here

Advertisement

Answer

If you just want to check if your form is valid, use checkValidity(). If you want some custom handling, you could try selecting all the possible fields and then loop over them to validate the field individually.

For instance: (this list is not complete)

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement