Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Built-in HTML5 form validation for individual fieldset?

Is it possible to use browsers' built-in HTML5 form validation on an individual fieldset?

I'm building a multi step form but would like to use the browser's built-in validation tool to detect and display validation messages for inputs on the currently displayed fieldset, rather than on the entire form (Chrome gives an error here because the input in the hidden fieldset is not focusable, besides I don't want it to validate the second input when the first button is clicked)

I know you can do custom JS validation and error display, but I want to know if it's possible to use the browser validation on an individual fieldset.

const form = document.getElementById('form')
const first = document.getElementById('first')
const second = document.getElementById('second')


form.addEventListener('submit', (e) => {
e.preventDefault();
first.style.display = 'none';
second.style.display = 'block';

})
<form id="form">
    <fieldset id="first">
        <legend>first</legend>
        <input name="firstInput" required>
        <button>next step</button>
    </fieldset>

    <fieldset id="second" style="display: none;">
        <legend>second</legend>
        <input required name="secondInput">
        <button>Submit</button>
    </fieldset>
</form>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've managed to get what I need like this.

  • Remove required from all inputs that are not displayed when the page loads
  • When the function to show the next fieldset runs, it sets all the inputs on the newly displayed fieldset (except checkboxes) as required
  • If you want to move back to the previous fieldset, the function removes required from the inputs in the fieldset you've just hidden, otherwise you wouldn't be able to move forward again (because the <input required> in the hidden fieldset cannot be focused, the htmll5 validation breaks)
  • At the last step, the form submits as expected

I'm including my code in case it can help someone.

form = document.getElementById('Form');
fieldsets = document.querySelectorAll('fieldset');
back = document.getElementById('back');
nextORsubmit = document.getElementById('nextORsubmit');



let i = 0;

form.addEventListener('submit', (e) => {

    if (i < fieldsets.length - 1) {
        e.preventDefault();

        console.log('Validate, but don\'t send form');
        fieldsets[i].style.display = 'none';
        fieldsets[i+1].style.display = 'block';
        back.style.display = 'inline';
        i++;

        // set required on current fieldset inputs, except if they're checkboxes
        fieldsets[i].querySelectorAll('input:not([type="checkbox"])').forEach (el => {
            el.required = true;
        })
    }
});

back.addEventListener('click', () => {
    console.log('going back a step');
    fieldsets[i].style.display = 'none';
    fieldsets[i-1].style.display = 'block';
    i--;

    // remove required on inputs from the next fieldset that we've just hid
    fieldsets[i+1].querySelectorAll('input:not([type="checkbox"])').forEach (el => {
        el.required = false;
    })

    // remove back button when you go back to the first step
    if (i == 0) {
        back.style.display = 'none';
    }
})
#back,
fieldset:not(:first-of-type) {
    display: none;
}
<form id="Form">
    <fieldset>
        <legend>first</legend>
        <input name="firstInput" required>
    </fieldset>

    <fieldset>
        <legend>second</legend>
        <input name="secondInput">
    </fieldset>
    
    <fieldset>
        <legend>third</legend>
        <input name="thirdInput">
    </fieldset>
    
  <button id="back" type="button">Back</button><br>
  <button id="nextORsubmit">Next</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda