Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

samueldev

Validar Select

Recommended Posts

Fiz o seguinte select, e gostaria de fazer uma validação onde se o select não estiver selecionado apareça uma mensagem, mas queria fazer isso usando a classe do select .


<label for="disciplina_professor">Disciplina:

<select class="sel_disciplina" multiple="multiple" name="disciplina_professor[]" >


<?php

//codigo que exibe disciplina

?>

</select></label>


Alguém poderia dar uma ajudinha ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

 

if($(".sel_disciplina").val()==""){

/// Retorno

}

Se não for isso me avise.

Olá Boa noite, tentei usar este código e também não deu certo, vou postar minha sintaxe pra vc da um olhada e me dizer mais ou menos onde posso estar errando

 

<html lang="PT-BR" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
$(function(){
$("select.sel_disciplina").multiselect();
});
function valida_envia(){
//valido o nome
if (document.cadastro.nome_professor.value.length==0){
alert("Tem que escrever seu Nome")
document.cadastro.nome.focus()
return 0;
}
if(document.cadastro.nome_professor.value==""||
document.cadastro.nome_professor.value.length < 20){
alert("Preencha este campo corretamente!");
document.cadastro.nome_professor.focus();
return false;
}
if (document.cadastro.cpf_professor.value.length==0){
alert("Digite seu CPF")
document.cadastro.cpf_professor.focus()
return 0;
}
if(document.cadastro.cpf_professor.value==""||
document.cadastro.cpf_professor.value.length < 14){
alert("Preencha este campo corretamente!");
document.cadastro.cpf_professor.focus();
return false;
}
if (document.cadastro.rg_professor.value.length==0){
alert("Tem que escrever seu RG")
document.cadastro.rg_professor.focus()
return 0;
}
if(document.cadastro.rg_professor.value==""||
document.cadastro.rg_professor.value.length < 14){
alert("Preencha este campo corretamente!");
document.cadastro.rg_professor.focus();
return false;
}
if (document.cadastro.titulo_professor.value.length==0){
alert("Tem que escrever o numero do titulo")
document.cadastro.titulo_professor.focus()
return 0;
}
if(document.cadastro.titulo_professor.value==""||
document.cadastro.titulo_professor.value.length < 14){
alert("Preencha este campo corretamente!");
document.cadastr.titulo.focus();
return false;
}
if (document.cadastro.ctps_professor.value.length==0){
alert("Tem que escrever o numero da sua CTPS")
document.cadastro.ctps_professor.focus()
return 0;
}
if(document.cadastro.ctps_professor.value==""||
document.cadastro.ctps_professor.value.length < 18){
alert("Preencha este campo corretamente!");
document.cadastro.ctps_professor.focus();
return false;
}
if (document.cadastro.dia.selectedIndex==0){
alert("Selecione sua Data de Nascimento")
document.cadastro.dia.focus()
return 0;
}
if (document.cadastro.mes.selectedIndex==0){
alert("Selecione sua Data de Nascimento")
document.cadastro.mes.focus()
return 0;
}
if (document.cadastro.ano.selectedIndex==0){
alert("Selecione sua Data de Nascimento")
document.cadastro.ano.focus()
return 0;
}
if (document.cadastro.tipoSangue_professor.selectedIndex==0){
alert("Selecione o seu tipo de sanguineo")
document.cadastro.tipoSangue_professor.focus()
return 0;
}
if(document.getElementById("masc").checked==true && document.cadastro.reservista_professor.value==0){
alert("Preencha a reservista")
document.cadastro.reservista_professor.focus()
return 0;
}
if (document.cadastro.estado_professor.selectedIndex==0){
alert("Selecione o seu Estado")
document.cadastro.estado_professor.focus()
return 0;
}
if (document.cadastro.cidade_professor.selectedIndex==0){
alert("Selecione seu municipio")
document.cadastro.cidade_professor.focus()
return 0;
}
if (document.cadastro.endereco_logra_professor .selectedIndex==0){
alert("Selecione o logradouro")
document.cadastro.endereco_logra_professor.focus()
return 0;
}
if (document.cadastro.endereco_professor.value.length==0){
alert("Tem que escrever seu Endereco")
document.cadastro.endereco_professor.focus()
return 0;
}
if (document.cadastro.nCasa_professor.value.length==0){
alert("Tem que escrever o numero da sua casa")
document.cadastro.nCasa_professor.focus()
return 0;
}
if (document.cadastro.bairro_professor.value.length==0){
alert("Tem que escrever o nome do seu bairro")
document.cadastro.bairro_professor.focus()
return 0;
}
if (document.cadastro.cep_professor.value.length==0){
alert("Tem que escrever seu CEP")
document.cadastro.cep_professor.focus()
return 0;
}
if(document.cadastro.cep_professor.value==""||
document.cadastro.cep_professor.value.length < 9){
alert("Preencha este campo corretamente!");
document.cadastro.cep_professor.focus();
return false;
}
if (document.cadastro.telefoneCel_professor.value.length==0){
alert("Tem que escrever o numero do seu telefone celular")
document.cadastro.telefoneCel_professor.focus()
return 0;
}
if(document.cadastro.telefoneCel_professor.value==""||
document.cadastro.telefoneCel_professor.value.length < 13){
alert("Preencha este campo corretamente!");
document.cadastro.telefoneCel_professor.focus();
return false;
}
if (document.cadastro.nomeUser_professor.value.length==0){
alert("Tem que escrever o nome de usuário")
document.cadastro.nomeUser_professor.focus()
return 0;
}
if (document.cadastro.email_professor.value.length==0){
alert("Tem que escrever o email")
document.cadastro.email_professor.focus()
return 0;
}
if( document.cadastro.email_professor.value=="" ||
document.cadastro.email_professor.value.indexOf('@')==-1 ||
document.cadastro.email_professor.value.indexOf('.')==-1 ){
alert( "Preencha campo E-MAIL corretamente!" );
document.cadastro.email_professor.focus();
return false;
}
if (document.cadastro.senha_professor.value.length==0){
alert("Tem que escrever a senha")
document.cadastro.senha_professor.focus()
return 0;
}
if(document.cadastro.senha_professor.value==""||
document.cadastro.senha_professor.value.length < 8){
alert("Preencha este campo corretamente!");
document.cadastro.senha_professor.focus();
return false;
}
if (document.cadastro.confirma_senha_professor.value==0 ||
document.cadastro.confirma_senha_professor.value.length < 6) {
alert("Preencha a confirmação de senha corretamente.");
document.cadastro.confirma_senha_professor.focus();
return false;
}
if (document.cadastro.senha_professor.value!=document.cadastro.confirma_senha_professor.value) {
alert("A senha e a confirmação tem de ser iguais.");
document.cadastro.confirma_senha_professor.focus();
return 0;
}
var Option=false;
Option=validaOption(document.cadastro.sexo_professor);
if (Option==false) return false; //se for falso, sai da validação
else {
cadastro.submit();//se não cadastra
}
}
function validaOption(obj){
totOp=obj.length;
var i=0, totsel=0;
for (i=0;i<totOp;i++)
{
if (obj.checked==false)
totsel+=1;
}
if (totsel==totOp)
{
alert("Selecione seu sexo");
return false;
}
}
//abrir e fechar reservistas
function abrir() {
document.getElementById('teste').style.display = 'block';
}
function fechar() {
document.getElementById('teste').style.display = 'none';
}
function bloqLetras( campo ) {
campo.value = campo.value.replace( /[a-z]/gi,'' );
// <!--alert("Apenas números");-->
}
function bloqNum( campo ) {
campo.value = campo.value.replace( /[0-9]/gi,'' );
//<!--alert("Apenas números");-->
}
</script>
<script language="javascript" type="text/javascript">
function mascara_cpf(campo){
var cpf_professor = campo.value;
if(cpf_professor.length==3){
cpf_professor = cpf_professor + '.';
document.forms[0].cpf_professor.value = cpf__professor;
return true;
}
if(cpf_professor.length==7){
cpf_professor = cpf_professor + '.';
document.forms[0].cpf_professor.value = cpf_professor;
return true;
}
if(cpf_professor.length==11){
cpf_professor = cpf_professor + '-';
document.forms[0].cpf_professor.value = cpf_professor;
return true;
}
}
function mascara_rg(campo1){
var rg_professor = campo1.value;
if(rg_professor.length==10){
rg_professor = rg_professor + '-';
document.forms[0].rg_professor.value = rg_professor;
return true;
}
}
function mascara_cep(campo2){
var cep_professor = campo2.value;
if(cep_professor.length==5){
cep_professor = cep_professor + '-';
document.forms[0].cep_professor.value = cep_professor;
return true;
}
}
function mascara_tel(campo3){
var telefoneCel_professor = campo3.value;
if(telefoneCel_professor.length==0){
telefoneCel_professor = telefoneCel_professor + '(';
document.forms[0].telefoneCel_professor.value = telefoneCel_professor;
return true;
}
if(telefoneCel_professor.length==3){
telefoneCel_professor = telefoneCel_professor + ')';
document.forms[0].telefoneCel_professor.value = telefoneCel_professor;
return true;
}
if(telefoneCel_professor.length==8){
telefoneCel_professor = telefoneCel_professor + '-';
document.forms[0].telefoneCel_professor.value = telefoneCel_professor;
return true;
}
}
function mascara_tel_fixo(campo4){
var telefoneFixo_professor = campo4.value;
if(telefoneFixo_professor.length==0){
telefoneFixo_professor = telefoneFixo_professor + '(';
document.forms[0].telefoneFixo_professor.value = telefoneFixo_professor;
return true;
}
if(telefoneFixo_professor.length==3){
telefoneFixo_professor = telefoneFixo_professor + ')';
document.forms[0].telefoneFixo_professor.value = telefoneFixo_professor;
return true;
}
if(telefoneFixo_professor.length==8){
telefoneFixo_professor = telefoneFixo_professor + '-';
document.forms[0].telefoneFixo_professor.value = telefoneFixo_professor;
return true;
}
}
</script>
<script language="javascript">
function Security(val, tamanho) {
document.getElementById('mensagem').innerHTML = "";
document.getElementById('d_baixa').style.background = 'white';
document.getElementById('d_media').style.background = 'white';
document.getElementById('d_alta').style.background = 'white';
if( val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) != -1
||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[A-Z]/) != -1 && val.search(/[@!#$%&*+=?|-]/)
||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1 && val.search(/[0-9]/)
||val.length >= tamanho && val.search(/[@!#$%&*+=?|-]/) != -1 && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) )
{
document.getElementById('mensagem').innerHTML = "forte";
document.getElementById('d_baixa').style.background = 'green';
document.getElementById('d_media').style.background = 'green';
document.getElementById('d_alta').style.background = 'green';
}
else{
if( val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[A-Z]/) != -1
||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[0-9]/) != -1
||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1
||val.length >= tamanho && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) != -1
||val.length >= tamanho && val.search(/[A-Z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1
||val.length >= tamanho && val.search(/[0-9]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1){
document.getElementById('mensagem').innerHTML = "média";
document.getElementById('d_baixa').style.background = 'yellow';
document.getElementById('d_media').style.background = 'yellow';
document.getElementById('d_alta').style.background = 'white';
}
else {
if(val.length >= tamanho)
{
document.getElementById('mensagem').innerHTML = "fraca";
document.getElementById('d_baixa').style.background = 'red';
document.getElementById('d_media').style.background = 'white';
document.getElementById('d_alta').style.background = 'white';
}
}
}
}
</script>
<script type="text/javascript" src="../../js/jquery-2.0.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select[name=estado_professor]").change(function(){
$("select[name=cidade_professor]").html('<option value="0">Carregando...</option>');
$.post("../controllers/listagem_cidades.php",
{estado_cidades:$(this).val()},
function(valor){
$("select[name=cidade_professor]").html(valor);
}
)
})
})
</script>
<form method="post" action="../controllers/cadastrando_professor.php" name="cadastro" enctype="multipart/form-data" >
<font color="red">*</font> <font>Campo Obrigatório</font><br /><br />
FOTO:<input name="arquivo" type="file" class="multi" maxlength="5" accept="jpeg|jpg|gif|png|psd" /></br></br>
<label for="name"><input id="name" style="font-size:20px; height:30px;" class="btn_user" placeholder="Nome" type="text" name="nome_professor" size="60" onkeyup="bloqNum( this )" onkeydown="bloqNum( this )"autofocus /><font color="red">*</font> <span id="nameInfo"></span> </label></br></br>
<label for="cpf"><input id="cpf" style="font-size:20px; height:30px;" class="btn_user" placeholder="Cpf" type="text" name="cpf_professor" size="60" onkeypress="mascara_cpf(this);" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )" maxlength="14"/><font color="red">*</font> <span id="cpfInfo"></span></label></br></br>
<label for="rg"><input id="rg" style="font-size:20px; height:30px;" class="btn_user" placeholder="Rg" type="text" name="rg_professor" size="60" onkeypress="mascara_rg(this);" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )" maxlength="14"/><font color="red">*</font> <span id="rgInfo"></span></label><br/><br/>
<label for="titulo"><input id="titulo" style="font-size:20px; height:30px;" class="btn_user" placeholder="Título" type="text" name="titulo_professor" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )" size="60" maxlength="14"/><font color="red">*</font> <span id="tituloInfo"></span></label></br></br>
<label for="ctps"><input id="ctps" style="font-size:20px; height:30px;" class="btn_user" placeholder="CTPS" type="text" name="ctps_professor" size="60" maxlength="18" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )"/><font color="red">*</font> <span id="ctpsInfo"></span></label><br/><br/>
<div class="style_select_DataNascimento">
Data de Nascimento:
<select name="dia">
<option style="color:#BBB" value="0">Dia</option>
<?php
for ($d=1;$d<=31;$d++){
$zero = ($d<10) ? 0: '';
if($dia==$zero.$d){
echo '<option value="', $zero, $d, '">', $zero, $d, '</option>';
}
else{
echo '<option value="', $zero, $d, '">', $zero, $d, '</option>';
}
}
?>
</select>
 
<select name="mes">
<option style="color:#BBB" value="0">Mês</option>
<?php
for ($d=1;$d<=12;$d++){
$zero = ($d<10) ? 0: '';
if($dia==$zero.$d){
echo '<option value="', $zero, $d, '">', $zero, $d, '</option>';
}
else{
echo '<option value="', $zero, $d, '">', $zero, $d, '</option>';
}
}
?>
</select>
 
<select name="ano">
<option style="color:#BBB" value="0">Ano</option>
<?php
for($a=date('Y'); $a>=(date('Y')-100);$a--){
if($a == $ano){
echo '<option value="',$a,'">',$a,'</option>';
}
else{
echo '<option value="',$a,'">',$a,'</option>';
}
}
?>
</select>
<font color="red">*</font><br/><br/>
</div>
<div class="style_select_TipoSanguineo">
<label for="tipoSangue_professor">Tipo Sanguíneo: <select name="tipoSangue_professor">
<option style="color:#BBB" value="0">Selecione</option>
<?php
$sql = "SELECT * FROM tb_tiposangue ORDER BY nome_tiposangue ASC";
$qr = mysql_query($sql) or die(mysql_error());
while($ln = mysql_fetch_assoc($qr)){
echo '<option value="'.$ln['nome_tiposangue'].'">'.$ln['nome_tiposangue'].'</option>';
}
?>
</select><font color="red">*</font></label><br/><br />
<label for="sexo_professor" >Sexo:
<input type="radio" name="sexo_professor" value="M" id="masc" onclick="abrir();" />Masculino
<input type="radio" name="sexo_professor" value="F" id="femi" onclick="fechar();" />Feminino
<input type="radio" name="sexo_professor" value="O" id="outros" onclick="abrir();"/> Outros<font color="red">*</font></br>
<div id="teste" style="display:none;">
<br/>
<label for="reservista"><input id="reservista" style="font-size:20px; height:30px;" class="btn_user" placeholder="Reservista" type="text" size="27" name="reservista_professor" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )"/><font color="red">*</font> <span id="reservistaInfo"></span></label>
</div><br/></label>
</div>
<div class="style_select_EstadosCidades">
<label for="id_estados">UF:        <select name="estado_professor">
<option style="color:#BBB" value="0">Selecione um estado</option>
<?php
$sql = "SELECT * FROM tb_estados ORDER BY nome_estados ASC";
$qr = mysql_query($sql) or die(mysql_error());
while($ln = mysql_fetch_assoc($qr)){
echo '<option value="'.$ln['uf_estados'].'">'.$ln['nome_estados'].'</option>';
}
?>
</select><font color="red">*</font> </label><br /><br />
<label for="id_cidades">Cidade:
<select name="cidade_professor">
<option style="color:#BBB" value="0">Selicione um Estado Primeiro</option>
</select><font color="red">*</font></label><br /><br/>
</div>
<div class="style_select_Endereco">
<label for="endereco_professor">Endereço:
<select name="endereco_logra_professor">
<option style="color:#BBB" value="0">Selecione</option>
<?php
$sql = "SELECT * FROM tb_logradouro ORDER BY nome_logradouro ASC";
$qr = mysql_query($sql) or die(mysql_error());
while($ln = mysql_fetch_assoc($qr)){
echo '<option value="'.$ln['nome_logradouro'].'">'.$ln['nome_logradouro'].'</option>';
}
?>
</select> <font color="red">*</font>
</label>
<input style="font-size:20px; height:30px;" class="btn_user" placeholder="Endereço" type="text" size="20" name="endereco_professor" onkeyup="bloqNum( this )" onkeydown="bloqNum( this )" /> <font color="red">*</font></label>
<label><input style="font-size:20px; height:30px;" class="btn_user" placeholder="Nº da Casa" type="text" name="nCasa_professor" maxlength="6" size="8" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )" /><font color="red">*</font></label><br/><br/></div>
<label><input style="font-size:20px; height:30px;" class="btn_user" placeholder="Bairro" type="text" name="bairro_professor" size="27" maxlength="25"/><font color="red">*</font></label>
<label for="cep"><input id="cep" style="font-size:20px; height:30px;" class="btn_user" placeholder="Cep" type="text" name="cep_professor" size="27" onkeypress="mascara_cep(this) " maxlength="9" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )"/><font color="red">*</font></label><span id="cepInfo"></span><br/><br/>
<!--Foto:<input type="text" name="foto_professor"/><br/> -->
<label for="telefone"><input id="telefone" style="font-size:20px; height:30px;" class="btn_user" placeholder="Telefone celular" type="text" name="telefoneCel_professor" size="27" onkeypress="mascara_tel(this)" maxlength="13" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )"/><font color="red">*</font></label>
<label ><input style="font-size:20px; height:30px;" class="btn_user" placeholder="Telefone fixo" type="text" name="telFixo_professor" size="27" onkeypress="mascara_tel_fixo(this)" maxlength="13" onkeyup="bloqLetras( this )" onkeydown="bloqLetras( this )"/></label><font color="red">*</font><span id="telefoneInfo"></span><br/><br/>
<div class="style_select_Disciplina">
<label for="disciplina_professor">Disciplina:
<select class="sel_disciplina" multiple="multiple" name="disciplina_professor[]" >
<?php
if($sql = "SELECT * FROM tb_disciplina ORDER BY nome_disciplina ASC"){
$qr = mysql_query($sql) ;
while($ln = mysql_fetch_assoc($qr)){
echo '<option value="'.$ln['nome_disciplina'].'">'.$ln['nome_disciplina'].'</option>';}
}else{
echo "ERRO";
}
?>
</select><font color="red">*</font></label><br></br>
<div>
<label><input style="font-size:20px; height:30px;" class="btn_user" placeholder="Nome de Usuário" type="text" name="nomeUser_professor" size="60" onkeyup="bloqNum( this )" onkeydown="bloqNum( this )" /> <font color="red">*</font> </label></br></br>
<label for="email"><input id="email" style="font-size:20px; height:30px;" class="btn_user" placeholder="Email" size="60" type="email" name="email_professor"/><font color="red">*</font></label><span id="emailInfo"></span><br/><br/>
<label for="pass1"><input id="pass1" style="font-size:20px; height:30px;" class="btn_user" placeholder="Senha" size="27" type="password" name="senha_professor" onKeyUp="Security(this.value, 5);"/><font class="pass1X" color="red">*</font></label>
<div class="ForSenha">
<table cellpadding="0" cellspacing="0" style=" background:#FFF; BORDER-RIGHT:#000 0px solid; BORDER-TOP:#000 0px solid; BORDER-LEFT:#000 0px solid; BORDER-BOTTOM:#000 0px solid">
<tr>
<td width="50" align="center">
<div id="d_baixa"> 
</div>
</td>
<td width="50" align="center">
<div id="d_media"> 
</div>
</td>
<td width="64" align="center">
<div id="d_alta"> 
</div>
</td>
</tr>
</table>
<div id="mensagem"></div>
</div><span id="pass1Info"></span><br /><br />
<label for="pass2"><input id="pass2" style="font-size:20px; height:30px;" class="btn_user" placeholder="Confirmar Senha" size="27" type="password" name="confirma_senha_professor"/><font color="red">*</font> <span id="pass2Info"></span></label><br/><br/>
<input style="width:590px;" class="btn" type="button" value="Enviar" onClick="valida_envia();" name="enviar" />
</form>
Amigo peço desculpas pela desorganização do código, é por que estou criando um exemplo rapidamente para uns testes e não estou conseguindo validar a disciplina

Compartilhar este post


Link para o post
Compartilhar em outros sites

 

 

consegui assim

 

var estado = $('select[name=estado] option:checked').val();

 

if( ! estado ) {

alert('Selecione um Estado');

return;

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.