Jump to content
Sign in to follow this  
t0ze

Notificação de campo de preenchimento obrigatório - label

Recommended Posts

t0ze

Boa tarde,

Dada a minha inexperiencia com javascript recorro para vos perguntar e depois de 1001 tentativas frustadas.. de como posso fazer  aparecer um "*" indicativo de campo obrigatorio num formulário, atraves de javascript. Ou seja, se o respectivo campo estiver preenchido, o "*" não é apresentado, caso contrario apresenta o asterisco "*" e não deixa fazer submit do formulário.

Este é o pedaço de código que tenho actualmente;

<?php
include "../Config/init.php";
require_once('calendar/classes/tc_calendar.php');
?>
<html>
<head>
<title>Inserir Noticia</title>

<link href="calendar/calendar.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="calendar/calendar.js"></script>
<script>
function valida()
{
if(document.form1.title.value == '')
	{
	document.form1.title.value = 'Campo Obrigatório'; 
	checktitle.style.visibility="visible";
	return false;
	}
	else{checktitle.style.visibility="hidden"}

}


</script>
</head>
<body>
<?php
if (isset($_SESSION['EMAIL'])) //SE EXISTIR AUTENTICAÇÃO
{
echo ' Olá ' . $_SESSION['EMAIL'] . '.<br/>';
echo '<a href="../Admin/logout.php"> Logout</a>';

		if(isset($_GET['AddNews']))
		{
				$title = $_POST["title"];
				$description = $_POST["description"];
				$body = $_POST["body"];
				$date = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
				$consulta = "Insert into content (ContentTitle, ContentDescription, ContentBody, ContentDate) 
													values ('$title', '$description', '$body', '$date')";
				$resultado=mysql_query($consulta);
				header("Location: ListNews.php"); 



		}
		else
		{
	    
?>
<form id="form1" name="form1" method="post" onsubmit="return valida();" action="<?php echo $_SERVER['PHP_SELF']."?AddNews=1";?>">
  <p>
	<label for="textfield">Titulo</label>
	<input type="text" name="title" id="title" onBlur="return valida();" /> <label name= "checktitle">*</label>
  </p>
  <p>
	<label for="textfield2">Descricao</label>
	<input type="text" name="description" id="description" />
  </p>
  <p>
	<label for="textfield2">Conteudo</label>
	<input type="text" name="body" id="body" />
  </p>
  <p>

	<label for="textfield2">Data</label>

	<?php
	$myCalendar = new tc_calendar("date1", true, false);
	$myCalendar->setIcon("calendar/images/iconCalendar.gif");
	$myCalendar->setDate(date('d'), date('m'), date('Y'));
	$myCalendar->setPath("calendar/");
	$myCalendar->setYearInterval(2000, 2015);
	$myCalendar->dateAllow('2008-05-13', '2015-03-01');
	$myCalendar->setDateFormat('j F Y');
	$myCalendar->setAlignment('left', 'bottom');
	$myCalendar->setSpecificDate(array("2011-04-01", "2011-04-04", "2011-12-25"), 0, 'year');
	$myCalendar->writeScript();
	?><?php } ?>

  </p>
  <p>
	<input type="submit" name="button" id="button" value="Inserir" />
  </p>
</form>
<?php 
echo $_SERVER['PHP_SELF']."?AddNews=1";
?>

<?php	
} 
else //CASO NÃO ESTEJA AUTENTICADO
{
echo 'Esta é uma área reservada, só utilizadores podem ter acesso.';
}
?>
</html>

Share this post


Link to post
Share on other sites
cra2sh

Isto?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script>
function check()
{
var teste = document.getElementById('teste');
var teste_2 = document.getElementById('teste_2');

if (teste.value != '')
{
	teste_2.innerHTML = '';
}

else
{
	teste_2.innerHTML = '*';
};
};		
</script>
<body>
<input type="text" id="teste" onkeyup="check()"/> <span id="teste_2">*</span>
</body>
</html>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.