Validate Form with JavaScript (valid option selected from cbx)

HTML - this a simple html form, which I have to validate with js, please if some could help me...

<form>
<label class="ancho">Nombre</label>
<input id="name" class="bloque" type="text" placeholder="Nicolas">
<label class="ancho">Apellido</label>
<input id="surename" class="bloque" type="text" placeholder="Oyarzun">
<label class="ancho">Edad</label>
<input id="age" class="bloque" type="number" placeholder="27" min="1" max="110">
<label class="ancho">Carrera</label>
    <select id="cbxCarrera" class="bloque">
        <option id="op0">-- Seleccione una Carrera --</option>
        <option id="op1">Programacion</option>
    </select>
    <button class="bloque boton" onclick="formulario()">Enviar</button
</form>

JavaScript

<script>
    var nombre = document.getElementById("name");
    var apellido = document.getElementById("surename");
    var edad = document.getElementById("age");
    var carrera = document.getElementById("cbxCarrera");
    var opcion0 = document.getElementById("op0");
    var opcion1 = document.getElementById("op1");

    function formulario() {
        while (nombre != null || apellido != null || edad > 0) {
            if (document.getElementById("cbxCarrera").value != "0") {
                // just checking if displays
                alert("nombre: " + nombre\ "apellido: " + apellido\ "edad: " + edad\ "carrera: "
                    document.getElementById(id).optSelected);
            }
        } else {
            alert("Porfavor ingrese valores validos en los campos anteriores.");
        }
    }
</script>

how can I validate the option selected in the combobox? thanks!

1 answer

  • answered 2017-10-20 13:12 Ellisan

    You can get the selected option by using selectedIndex like this

    carrera.options[carrera.selectedIndex];
    

    And then do with it as you please,

    REFERENCE