How to validate that there are no combobox selected

0

Hello my question is that in what way can I validate that for the case that there is no combobox selected, throw an error.

Normally it is the other way around with the validations, where it is asked that there be at least one or several comboboxes selected, or text boxes or some variable, and then continue with the sending of the form, asking if the string is empty or not and commonly is for some specific control, which is identified by the name or id of a particular object.

The following example I have shows a bit to what I mean:

<script language="JavaScript"> /*NOTA:Los mensajes del alert no son importantes*/
  function validaSelects(){ 
  if (document.formAlertas.RutaAl.value == "")
     {
        alert("No sea pillo. Seleccione una opción");
        return false;

    } else if(document.formAlertas.UsuariosA.value == "" ) 
        {
            alert("Segunda Advertencia, debe seleccionar una opcion.");
            return false;
        }
        else if (document.formAlertas.RegionA.value=="")
            {
                alert("Tercera Advertencia, deberá seleccionar una opción.");
                return false;
            }
            else if (document.formAlertas.ClientesA.value=="") 
            {
                alert("Se lo advertimos ya no podrá continuar");
                return false;
            }
            else{
                document.formAlertas.submit(); 
            }
      }

   </script>

This example I did trying to achieve the final purpose, what it does is ask if any of those combobox was selected and then throw an error for each combobox that has not been selected.

Another way that I tried was that I put the first 2 combobox together and compared them with an OR, then with a yes-no I asked for the next 2 combobox and I also compared them with || and for each comparison he sent an error message, which was not what he was looking for as a final purpose either.

But indistinctly what I said at the beginning what I want to do is something completely different, the purpose is to do a validation where the error throws it as long as there is no no combobox selected, and then the case that there is At least 1 combo selected ( Anyone ) continue with the submission of the form. I hope with javascript, which is what I've been trying and it's simpler.

    
asked by M4uriXD 20.04.2018 в 21:44
source

2 answers

2

If you want to verify if all select have value you can go through the form where they are and go concatenated in a variable the value of them. If at the end of the chain length is 0, it means that none had value, but at least 1 was worth.

Assuming that this is more or less your html structure:

var form = document.querySelector("#form1");	
var validateForm = function(){
    var combos = document.querySelectorAll("select");
    var val = '';
    for(var i=0;i<combos.length;i++){
	    val += combos[i].value;
    }
    var result = (val.length > 0);
    if(!result){
    	alert("Debe seleccionar uno de los combobox");
    }
    return result;
}
<!doctype html>
	<html>
	<body>
	<form id="form1" onsubmit="Javascript: return validateForm();">
	<select id="combo1">
		<option value="">Seleccione 1</option>
		<option>Dariel</option>
	</select>
	<select id="combo2">
		<option value="">Seleccione 1</option>
		<option>Dariel</option>
	</select>
	<input type="submit" value="enviar" />
	</form>
	</body>
	</html>
    
answered by 20.04.2018 / 22:14
source
0

Here I show you a very practical solution, which is to go through the elements of the form and validate one by one verifying if the client has not selected a valid option of the select elements, you show him a generic message ( You mentioned that the message was not important ):

function validaSelects() {
   var combos = document.querySelectorAll("select");
    for (let index = 0; index < combos.length; index++) {
        if (!combos[index].value) {
            alert("Debe completar los campos antes de continuar");
            return;
        }
    }
    document.formAlertas.submit();
}
    
answered by 20.04.2018 в 22:22