Duvida java script [desculper se aki nao for o lugar certo]

tenho o seguinte codigo em java script
so q ele nao esta validando as informacoes como deveria
ja vi tdo mas nao vi o erro alguem pode me ajudar

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pagina de formulario</title>
<style type="text/css">

.form{

background: red; 
font-family: fantasy;
font-weight: bold;
font-style: italic;
color: green;
font-size: 24px;
}
</style>

<script type="text/javascript">

function validar(){

var i=document.getElementById("sexo").selectedIndex;
var tst=true;
var saida="os campos sao obrigatorios ";

if(document.getElementById("nome").value==""){
	saida += "\nNome";
	tst=false;
}	

if(document.getElementsByName("sexo").options[i].value==""){

	saida="\nsexo";
	tst=false;
}

if(document.getElementById("sobrenome").value==""){
	saida += "\nsobrenome";
	teste = false;
}	


if(tst){
		alert("daddos salvos ok!!");
		return true;}
	else{
alert(saida);
return false;
		}
</script>

</head>
<body >
<form id="formulario 1" name="formulario 1">
<table>
<tr>
<td class="form">
 
 nome:
</td>
<td class="form">
 
 <input type="text" name="nome" id="nome">
</td>


</tr>

<tr>
<td class="form">
 
 sobrenome:
</td>
<td class="form">
 
 <input type="text" name="sobrenome" id="sobrenome ">
 
</td>


</tr>


<tr>
<td class="form">
 
 sexo:
</td>
<td class="form">
 
 <select id="sexo" name="sexo">
 <option> -------Selecione--------</option>
 <option value="masculino"> Homem</option>
 <option value="feminino"> mulher</option>
 
 </select>
</td>


</tr>

<tr>
<td>
decricao pessoal:
</td>

<td>
<textarea class="form" rows="30px" cols="47px" id="descricao" name="descricao" > 


</textarea>
</td>


</tr>

<td>
<input type="submit" value="guardar"  id="guardar" name="guardar" onclick="validar();"> 
</td>







<td class="form">
 
 <input type="reset" name="reset" id="reset " value="limpar">
 
</td>


</tr>



</table>
</form>
</body>
</html>

o sexo , nome e obrigatorio

Compare com o seu código e faça uns testes.
Qualquer dúvida, estou aqui!

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pagina de formulario</title>
<style type="text/css">

.form{

background: red; 
font-family: fantasy;
font-weight: bold;
font-style: italic;
color: green;
font-size: 24px;
}
</style>

<script type="text/javascript">

function validar(){


var tst = true;
var saida="os campos sao obrigatorios ";

if(document.getElementById("nome").value == ""){
	saida = saida + "\nNome";
	tst = false;
}	


if (document.getElementById("sexo").value == "-------Selecione--------"){
	saida += "\nsexo";
	tst = false;
}

if(document.getElementById("sobrenome").value==""){
	saida += "\nsobrenome";
	tst = false;
}	

if(tst == true){
	alert("daddos salvos ok!!");
	return true;
}else{
	alert(saida);
	return false;
}
}
</script>

</head>
<body >
<form id="formulario1" name="formulario1" action="#">
<table>
<tr>
<td class="form">nome:</td>
<td class="form"><input type="text" name="nome" id="nome"></td>
</tr>
<tr>
<td class="form">sobrenome:</td>
<td class="form"><input type="text" name="sobrenome" id="sobrenome"></td>
</tr>
<tr>
<td class="form">sexo:</td><td class="form">
 <select id="sexo" name="sexo">
 <option>-------Selecione--------</option>
 <option value="masculino">Homem</option>
 <option value="feminino">Mulher</option>
 </select>
</td>
</tr>
<tr>
<td>decricao pessoal:</td>
<td><textarea class="form" rows="30px" cols="47px" id="descricao" name="descricao" ></textarea>
</td>
</tr>
<td><input type="button" value="guardar"  id="guardar" name="guardar" onclick="validar();"> </td>
<td class="form"><input type="reset" name="reset" id="reset " value="limpar"></td>
</tr>
</table>
</form>
</body>
</html>

Abs
Rodrigo

Boa noite a todos.

[quote=rsantik]Compare com o seu código e faça uns testes.
Qualquer dúvida, estou aqui!

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pagina de formulario</title>
<style type="text/css">

.form{

background: red; 
font-family: fantasy;
font-weight: bold;
font-style: italic;
color: green;
font-size: 24px;
}
</style>

<script type="text/javascript">

function validar(){


var tst = true;
var saida="os campos sao obrigatorios ";

if(document.getElementById("nome").value == ""){
	saida = saida + "\nNome";
	tst = false;
}	


if (document.getElementById("sexo").value == "-------Selecione--------"){
	saida += "\nsexo";
	tst = false;
}

if(document.getElementById("sobrenome").value==""){
	saida += "\nsobrenome";
	tst = false;
}	

if(tst == true){
	alert("daddos salvos ok!!");
	return true;
}else{
	alert(saida);
	return false;
}
}
</script>

</head>
<body >
<form id="formulario1" name="formulario1" action="#">
<table>
<tr>
<td class="form">nome:</td>
<td class="form"><input type="text" name="nome" id="nome"></td>
</tr>
<tr>
<td class="form">sobrenome:</td>
<td class="form"><input type="text" name="sobrenome" id="sobrenome"></td>
</tr>
<tr>
<td class="form">sexo:</td><td class="form">
 <select id="sexo" name="sexo">
 <option>-------Selecione--------</option>
 <option value="masculino">Homem</option>
 <option value="feminino">Mulher</option>
 </select>
</td>
</tr>
<tr>
<td>decricao pessoal:</td>
<td><textarea class="form" rows="30px" cols="47px" id="descricao" name="descricao" ></textarea>
</td>
</tr>
<td><input type="button" value="guardar"  id="guardar" name="guardar" onclick="validar();"> </td>
<td class="form"><input type="reset" name="reset" id="reset " value="limpar"></td>
</tr>
</table>
</form>
</body>
</html>

Abs
Rodrigo[/quote]

Rsantik, permita-me fazer uma pequena correção no seu código.

Com essa tag.

...<input type="button" value="guardar"  id="guardar" name="guardar" onclick="validar();">....

Voce só está apenas validando dados, porém não está enviando para lugar nenhum, ou seja, enviando um “Submit”.

Por isso, dentro da function validar() no javascript, modifique este código:

     .....
     if(tst == true){  
         alert("daddos salvos ok!!");
         var frm = document.getElementById("formulario1");
         frm.submit();
         return true;  
     }else{  
         alert(saida);  
         return false;  
     }
     .....

[quote]Rsantik, permita-me fazer uma pequena correção no seu código.

Com essa tag.

...<input type="button" value="guardar"  id="guardar" name="guardar" onclick="validar();">....

Voce só está apenas validando dados, porém não está enviando para lugar nenhum, ou seja, enviando um “Submit”.

Por isso, dentro da function validar() no javascript, modifique este código:

     .....
     if(tst == true){  
         alert("daddos salvos ok!!");
         var frm = document.getElementById("formulario1");
         frm.submit();
         return true;  
     }else{  
         alert(saida);  
         return false;  
     }
     .....

[/quote]

Perfeito discorpio, bem colocado!
Caso ele for querer validar os campos via javascript, então terá que dar o submit também via javascript, conforme mostra seu código.

Perfeito!

Abs
Rodrigo