Ir para conteúdo

POWERED BY:

Arquivado

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

igor94

Limpar campos e não permitir caracteres e nem campo vazio

Recommended Posts

Bom fiz o código, mas não sei pq não está funcionando, gostaria da ajuda de vcs !

O objetivo do código e de, que quando o usuario clique no campo onde está o value HH e MM, limpe os campos com um clique e que na hora de cadastrar exiba uma menssagem caso o usuario digite caracteres ou deixe o campo vazio , permitindo somente NUMEROS!

 

 

 

<html>
<head>
<title>Cadastrando Turno</title>
</head>
<body>

<script type="text/javascript">
<!--
function limpahorainicio (){
document.turno.hrE.value="";
document.turno.hrE.focus();
return true;
}
function limpaminutoinicio (){
document.turno.mnE.value="";
document.turno.mnE.focus();
return true;
}

function limpahorafinal (){
   document.turno.hrS.value="";
document.turno.hrS.focus();
return true;
}

function limpaminutofinal (){
document.turno.mnE.value="";
document.turno.mnE.focus();
return true;
}


function Turno(){
hr1 = document.turno.hrE.value;
mn1 = document.turno.mnE.value;
hr2 = document.turno.hrS.value;
mn2 = document.turno.mnS.value;

if (hr1 == "") {
	alert("O campo está vazio, informe a hora.");
	document.turno.hrE.focus();
	return false;
}
if (isNaN(hr1) == true){
    alert("Digite somente números.");
	document.turno.hrE.focus();
	return false;
}	
if (mn1 == ""){
	alert("O campo está vazio, informe os minutos.");
	document.turno.mnE.focus();
	return false;
}
if(isNaN(mn1) == true){
	alert("Digite somente números.");
	document.turno.mnE.focus();
	return false;
}
if (hr2 == "") {
	alert("O campo está vazio, informe a hora.");
	document.turno.hrS.focus();
	return false;
}
if (isNaN(hr2) == true){
    alert("Digite somente números.");
	document.turno.hrS.focus();
	return false;
}
if (mn2 == ""){
	alert("O campo está vazio, informe os minutos");
	document.turno.mnS.focus();
	return false;
}
if(isNaN(mn2) == true){
	alert("Digite somente números.");
	document.turno.mnS.focus();
	return false;
}
}
//-->
</script>


<form name"turno" method="post" action="turno_cad_resposta.php" onsubmit="return Turno(this)"/>
<table width="286" height="157" border="1" bordercolor="#000000" bordercolor="#FFFFFF">
<tr>
<td align="center">Cadastrando Turno</td></tr>
<tr>
<td>
Inicio:<input type="text" name="hrE" value="HH" maxlength="2" size="3" onclick="limpahorainicio()"/>
<input type="text" name="mnE" value="MM" maxlength="2" size="3" onclick="limpaminutoinicio()" /></td>
</tr>
<tr>
<td>Término: <input type="text" name="hrS" value="HH" maxlength="2" size="3" onclick="limpahorafinal()"/>
<input type="text" name="mnS" value="MM" maxlength="2" size="3" onclick="limpaminutofinal()"/></td>
</tr> 
<tr>
<td><input type="submit" value="Cadastrar" /> <input type="reset" value="Limpar"  /></td>
</tr>
</table>
</form>

</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

ver esse código que fiz :thumbsup:/> .

 

<!DOCTYPE HTML>
<html lang="pt_br">
<head>
<meta charset="utf-8"/>
<title>teste</title>
<script type="text/javascript">
window.onload = function () { 

var hora = document.getElementById("turno").getElementsByTagName("input");

	for(i=0; i<hora.length; i++){
		hora[i].onfocus = function() {
			this.value = "";
		}
	}

	for(i=0; i<hora.length; i++){
		hora[i].onblur = function() {
			var campoHora = this.value;
			var validar = /^\d+$/;
			resultado = validar.test(campoHora);

			if(resultado){
				this.value;
			}else if (!resultado && hora[0] == this || hora[2] == this) {
			alert("Digite só números.")
			this.value = "HH";
			}
			else if (!resultado && hora[1] == this || hora[3] == this) {
			alert("Digite só números.")
			this.value = "MM";
			}

		}
	}	


}

</script>
</head>
<body>
<form name="turno" method="post" id="turno" action="turno_cad_resposta.php"/>
<table width="286" height="157" border="1" bordercolor="#000000" bordercolor="#FFFFFF">
<tr>
<td align="center">Cadastrando Turno</td></tr>
<tr>
<td>
Inicio:<input type="text" name="hrInicio"  value="HH" maxlength="2" size="3"/>
<input type="text" name="mnE" value="MM"  maxlength="2" size="3" /></td>
</tr>
<tr>
<td>Término: <input type="text" name="hrS" value="HH" maxlength="2" size="3"/>
<input type="text" name="mnS" value="MM" maxlength="2"  size="3" /></td>
</tr> 
<tr>
<td><input type="submit" value="Cadastrar" /> <input type="reset" value="Limpar"  /></td>
</tr>
</table>
</form>
</body>
</html>	



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.