Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

294
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda