Validate fields with selects (Combobox)

0

I have the code below that generates rooms in my selects.

 str += '<select class="select-group" id="selQuartos">';
 str += '<option>Qtd. quartos</option>';

 for (var i = 1; i < $("#Quartos").val() + 1; i++) {

     if (i == 1) str += '<option>' + i + ' quarto</option>';
     else str += '<option>' + i + ' quartos</option>';

     if (i == $("#Quartos").val()) break;
 }

 str += '</select>';

I have this javascript function that validates whether there are checkboxes checked or not. I would add in this validation if there are rooms selected in my combos above, but I can not do it. Checkboxes get the values checked and you're done. Now how to do with Combos? I tried this way: if($("selQuartos").val() < 1) ... This did not work, because I can not bring the value of the Selector (Combobox). Below javascript to validate:

 function Submit() {
     var Apartmento = [];
     var retorno = false;
     for (var i = 0; i < filtroPesquisa.chkApartmento.length; i++) {
         if (filtroPesquisa.chkApartmento[i].checked) {
             Apartmento.push(filtroPesquisa.chkApartmento[i].value);
         }
     }
     if (Apartmento.length == 0) {
         for (var i = 0; i < filtroPesquisa.chkApartmento.length; i++) {
             filtroPesquisa.chkApartmento[i].style.color = "red";;
         }
         retorno = false;
     } else {
         retorno = true;
     }

So, I started doing what FS.DEV posted. If I comment the for and give false return, it does not go to another page, but if I leave the for even with return false, it goes to another page. As it calls another page, I can not catch it if there is or not javascript error. Here's how I'm doing. I put the return false just for testing.

function Submit() {

    var _selSeguro = document.getElementById('selSeguro');
    var _selIngresso = document.getElementById('selIngresso');
    var _selPasseio = document.getElementById('selPasseio');

    var selecionados = new Array();
    for(var i = 0; i <_selIngresso.length; i++)
        if(_selIngresso[i].selected)
            selecionados.push({'id':i,'valor':_selIngresso.value});


        return false;

    }

I noticed something important here. When the page comes in, comes the sliders closed. When I open a slider, it shows me the buttons and combos to fill. With the sliders open, the button to navigate the other page obeys the return of the function. This only does not happen when the sliders are closed. Does anyone know why?

I made that form and it did not work.

if ((_selIngresso.value == '') && (_selPasseio.value == '') && (_selSeguro.value == ''))
        return false;

If I do one by one, it works, but they all do not, because when I open a slider, the denials close and this validation only happens when the sliders are open. Anyone have any ideas how to do this?

I said slider, but now I see that ACCORDION is called the effects, ie when one opens the other closes. When it's closed, I can not take anything from it, nothing at all. Then it gives javascript error and the button calls the page.

    
asked by anonymous 15.04.2014 / 22:19

1 answer

1

It is not very clear what you asked, what I understood and you want to know if it is selected or a select one, if this is what you can do as follows ... I will use pure javascript, if necessary do the adaptations.

The first would be as follows. Replace the line

str += '<option>Qtd. quartos</option>';

by

str += "<option value=''>Qtd. quartos</option>";

and do the following validation:

<script>
 var sel = document.getElementById('selQuartos');
 if(sel.value == '')
  alert('Campo com valor inválido');

 //A outra forma seria a seguinte, que suporta select multiplos
 var selecionados = new Array();
 for(var i=0; i<sel.length; i++)
  if(sel[i].selected)
   selecionados.push({'id':i,'valor':sel.value});

 if(selecionados.length > 0){
  alert("Existe(m) selecionado(s)");
  alert("Primeiro valor: Posição, "+selecionados[0].id+", valor: "+selecionados[0].value)
 }else{
  alert("Nenhum Selecionado")
 }
</script>
    
16.04.2014 / 14:20