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

299
Visualizações
How to validate select fields in html, laravel and javascript

I'm trying to make sure every field is required but this code only works on "input" tags but not on the select and textarea tags.

Any help will be appreciated. Thanks!

HERE IS MY BLADE

<div class="tab">
    <div class="form-group mb-3">
        <select name="currency" class="choices form-control" id="devise{{ Auth::user()->cred->id }}"
                onchange="deviseSelect({{ Auth::user()->cred->id }})" required="true">
            <option value="" selected disabled>choose</option>
            <option value="USD">USD</option>
            <option value="FC">FC</option>
        </select>
    </div>
</div>

HERE IS MY JAVASCRIPT FOR THE "SELECT" HTML TAG

    <script>
        var currentTab = 0; // Current tab is set to be the first tab (0)
        showTab(currentTab); // Display the current tab
        function validateSelect()
        {
            // This function deals with validation of the form fields
            var x,z, i, valid = true;
            x = document.getElementsByClassName("tab");
            z = x[currentTab].getElementsByTagName("select");

            // A loop that checks every select field in the current tab:
            for (i = 0; i < z.length; i++) {
                var option = z.options[i];
                // If a field is empty...
                if (option.value == "") {
                    // add an "invalid" class to the field:
                    z[i].className += " required";
                    // and set the current valid status to false:
                    valid = false;
                }
            }
            // If the valid status is true, mark the step as finished and valid:
            if (valid) {
                document.getElementsByClassName("step")[currentTab].className += " finish";
            }
            return valid; // return the valid status
        }
    </script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I FOUND WHERE I'VE MISTAKEN

I just changed something inside the for loop and everything started working perfectly.

<script>
for (i = 0; i < z.length; i++) {
    // If a field is empty...
    if (z[i].value == "") {
        // add an "invalid" class to the field:
        z[i].className += " is-invalid";
        // and set the current valid status to false:
        valid = false;
    }
}
</script>

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