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

150
Visualizações
trying to make validation on radiobutton

I have a problem with validation on radiobutton.

I did try by having a loop to check if the radiobutton is checked or not but it turns out give delivery flag still true

        var optDelivery=document.getElementsByName("f__deliver");
        var deliveryFlag=false;
        for (var i=0;i<optDelivery.length;i++){
            if (optDelivery.checked){
                deliveryFlag=true;
            }
        }

        if (deliveryFlag===true){
            alert("AAAA");
            optDelivery.style.background="white";
            optDelivery.style.color="#000";
        } else{
            alert("A");
            optDelivery.style.background="#DE8971";
            optDelivery.style.color="#FFE9D6";
            optDelivery.focus();
            return false;
            
        }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to access each input radio by its index, also the condition needs to be inside the loop

Also instead of return falseset deliveryFlag to false

const optDelivery = document.getElementsByName("f__deliver");

for (let i = 0; i < optDelivery.length; i++) {
 
  const deliveryFlag = optDelivery[i].checked ? true : false;

  if (deliveryFlag === true) {
    console.log("AAAA");
    optDelivery[i].style.background = "white";
    optDelivery[i].style.color = "#000";
  } else {
    console.log("A");
    optDelivery[i].style.background = "#DE8971";
    optDelivery[i].style.color = "#FFE9D6";
    optDelivery[i].focus();
  }
}
<input type="radio" name="f__deliver" id="">
<input type="radio" name="f__deliver" id="">
<input type="radio" name="f__deliver" id="">
<input type="radio" name="f__deliver" id="" checked>
<input type="radio" name="f__deliver" id="">

about 4 years ago · Juan Pablo Isaza Relatório

0

var optDelivery=document.getElementsByName("f__deliver");
        var deliveryFlag=false;
        for (var i=0;i<optDelivery.length;i++){
            if (optDelivery[i].checked){
                deliveryFlag=true;
            }
            
            if (deliveryFlag===true){
              alert("AAAA");
              optDelivery[i].style.backgroundColor="white";
              optDelivery[i].style.color="#000";
              optDelivery[i].focus();    
            }else{
              alert("A");
              optDelivery[i].style.backgroundColor="#DE8971";
              optDelivery[i].style.color="#FFE9D6";
                     
            }
            deliveryFlag = false;
         }

       
<input type='radio' name = 'f__deliver'>
<input type='radio' name = 'f__deliver' checked>
<input type='radio' name = 'f__deliver'>

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