Jump to content

Recommended Posts

Posted

Boa tarde, queria colocar os inputs obrigatorios a cor vermelha quando nao é colocada dados. Mas nao estou a conseguir. Aqui ficam os codigos.

registar.php


<?php

include "config.php";
include "template.php";


$error = "";
$success = "";
if (!empty($_POST['register'])) {
if (!$_POST['username'] || !$_POST['password'] || !$_POST['email'] || !$_POST['repassword']) {
print "<div align='center'><h4>Tem de preencher todos os campos.</h4></div>";

exit;
}

$username = $_POST['username'];

$dupe1 = mysql_num_rows(mysql_query("select * from users where username='$username'"));

if ($dupe1 > 0) {
print "<div align='center'><h4>Esse username ja esta em uso.</h4></div>";

exit;
}

$email = $_POST['email'];

$dupe2 = mysql_num_rows(mysql_query("select * from users where email='$email'"));
if ($dupe2 > 0) {
print "<div align='center'><h4>Esse email ja esta a ser usado.</h4></div>";

exit;
}
elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
print "<div align='center'><h4>Email nao é valido.</h4></div>";

}

//check if passwords are the same
if ($_POST['password'] != $_POST['repassword']) {
print "<div align='center'><h4>Passwords nao coincidem.</h4></div>";

exit;
}

if (empty($error )) {
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']); // escaping not needed when hashing. Should use a salt for md5
$email = mysql_real_escape_string($_POST['email']);


$id_usertipo = 4; // este é o id com que o user fica quando se regista
$registodata = date("d F Y");
if (empty($error)) {
 $registerquery = mysql_query("INSERT INTO users (username, password, email) VALUES('".$username."', '".$password."', '".$email."')");
	if($registerquery) {
	 $success .= "<div align='center'>A sua conta foi criada. Por favor <a href=\"login.php\">clique aqui para login</a>.</div>";
	} else {
	 $error .= "<div align='center'>Desculpe, o registo falhou. Por favor tente de novo.</div>";
   }
}
}
}
?>

<?php
if (!empty($error)) {
?>
<h1 align='center'>Erro</h1>
<?php echo $error; ?>
<?php
}elseif (!empty($success)) {
echo $success;
}else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%22>
<html xmlns="http://www.w3.org/1999/xhtml%22>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>Registo</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script language="javascript" src="funcoes.js"></script>
<script type="text/javascript">getAction();</script>
<style type="text/css">
div.dadospessoais {
float:left;
}
div.dadospessoais2 {
  margin: -250px;
  padding-right: 10px;

}
</style>
</head>
<body>
<div id="main">

<div id="login_wrapper">
<div id="login_box">
<form method="post" action="registar.php" name="registerform" id="registerform">
<div class="dadospessoais">
<table>
<tr>
								 <td>
									Username:
									</td>
									<td>
									<input id="username" name="username" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Password:
									</td>
									<td>
									<input id="password" name="password" type="password" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Confirme a Password:
									</td>
									<td>
									<input id="repassword" name="repassword" type="password" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Email:
									</td>
									<td>
									<input id="email" name="email" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Nome Completo:
									</td>
									<td>
									<input name="nomecompleto" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Apelido:
									</td>
									<td>
									<input name="apelido" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Nº BI:
									</td>
									<td>
									<input name="numbi" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Morada:
									</td>
									<td>
									<input name="morada" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Código Postal:
									</td>
									<td>
									<input name="codpostal" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Localidade:
									</td>
									<td>
									<input name="localidade" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Distrito:
									</td>
									<td>
									<input name="distrito" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Idade:
									</td>
									<td>
									<input name="idade" type="text" class="input" size="25"/>
									</td>
								</tr></table></div>
<div class="myuploadsSeparator" style="height: 250px; margin-left: 20px; margin-right: 10px;"></div>
<div class="dadospessoais2"><table><tr>
								 <td>
									Telefone:
									</td>
									<td>
									<input name="telefone" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr><td>Estado Civil:</td><?php
$q = 'SELECT id, estadocivil_categoria FROM estadocivilselect';
$result = mysql_query($q);
echo '<td><select name="referente">'; $c='';
while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['estadocivil_categoria'].'</option>';
}
echo '</select></td><tr>'; ?>
<tr><td>Tipo de Habitação</td><?php
$q = 'SELECT id, tipohabitacao_categoria FROM tipohabitacaoselect';
$result = mysql_query($q);
echo '<td><select name="referente">'; $c='';
while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['tipohabitacao_categoria'].'</option>';
}
echo '</select></td></tr>'; ?>
<tr>
								 <td>
									Carro:
									</td>
									<td>
									<input name="carro" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Marca:
									</td>
									<td>
									<input name="marca" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Profissão:
									</td>
									<td>
									<input name="profissao" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Profissão que gostava de ter:
									</td>
									<td>
									<input name="profissaogostava" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Profissões que sei e posso desempenhar:
									</td>
									<td>
									<input name="profissoesqsei" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Empregador:
									</td>
									<td>
									<input name="empregador" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Actividades Extra:
									</td>
									<td>
									<input name="actividadextra" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Local Onde Pratica:
									</td>
									<td>
									<input name="localondepratica" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Nº Agregado Familiar:
									</td>
									<td>
									<input name="agregadofamiliar" type="text" class="input" size="25"/>
									</td>
								</tr>
</table>
<input type="submit" name="register" id="register" value="Register" />
</form></div></div></div>

<?php
}
?>

</div>
</body>
</html>

funcoes.js


function validateForm(){
var error = 0;

$('#usernameInput').css('color', '#535353');
$('#passwordInput').css('color', '#535353');
$('#emailInput').css('color', '#535353');

if($('#username').val() == ''){
$('#useranmeInput').css('color', 'red'); error = 1;
}
if($('#password').val() == ''){
$('#passwordInput').css('color', 'red'); error = 1;
}
if($('#email').val() == ''){
$('#emailInput').css('color', 'red'); error = 1;
}

if(error == 0)
return true;
else
return false;
}

Espero por ajuda obrigado.

Posted (edited)

Podes fazer algo do género:


$('#registerform').submit(function() {

var error = 0;

$('#username').css('color', '#535353');
$('#password').css('color', '#535353');
$('#email').css('color', '#535353');

if($('#username').val() == ''){
$('#useranme').css('color', 'red'); error = 1;
}
if($('#password').val() == ''){
$('#password').css('color', 'red'); error = 1;
}
if($('#email').val() == ''){
$('#email').css('color', 'red'); error = 1;
}

if(error == 0)
return true;
else
return false;

});

Edited by KiNgPiTo

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
×
×
  • 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.