Validation of select

0

I have the following HTML

<select class="cadastroDireitaInput cadastroSiteSexo" id="sexo" name="sexo">
  <option value="">Selecione</option>
  <option value="Feminino">Feminino</option>
  <option value="Masculino">Masculino</option>
</select>

I have a JS that checks if the fields are filled, such as this line of code:

if($.trim($('#nomeFormUS').val())=='')alert('Informe seu nome!');

In this case, it checks if an input is filled in.

Now, how do I check if any of this Select's option has been selected?

    
asked by anonymous 11.08.2014 / 19:18

3 answers

1

You can also use .val() or use pure javascript .value

Take a look at this example that alerts "false" when you choose the first option and "true" on the others:

$('#sexo').on('change', function(){
    $('#resultado').text(!!this.value);
});

Demo: jsFiddle

So your code could be: ( jsFiddle )

 if(!this.value) alert('Informe seu sexo!');
    
11.08.2014 / 19:22
1

But the correct one would already be to bring some sex as standard ... simple problem and less to worry about

  $('#sexo').change(function(){
       var value = $(this).val();
          if(value == ''){
            alert('Escolha um sexo');        
        }
    });
    
12.08.2014 / 21:14
1

I use this way:

<script>
if(nome_formuario.nome_do_select.selectedIndex==0){
            alert("Informe o status do contrato");
            form.st_contrato.focus();
            return false;
        }
</script>
    
12.08.2014 / 22:00