Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Estou tentando comparar uma senha, mas não está dando certo, sempre passa mesmo a senha sendo diferente, o restante da validação funciona. como campos em branco e valores minimos.
Segue o código
html
<div class="inputs">
<label>Pass </label>
<input name="pass" type="text" class="required" id="pass" size="30" />
</div>
<div class="inputs">
<label>cPass </label>
<input name="cpass" type="text" class="required" id="cpass" size="30" />
</div>
validação
$(document).ready(function() {
$('form #response').hide();
$('#submit').click(function(e) {
// prevent forms default action until
// error check has been performed
e.preventDefault();
// grab form field values
var valid = '';
var required = ' é necessário.';
var name = $('form #name').val();
var email = $('form #email').val();
var pass = $('form #pass').val();
var cpass = $('form #cpass').val();
var message = $('form #message').val();
var honeypot = $('form #honeypot').val();
var humancheck = $('form #humancheck').val();
// perform error checking
if (name = '' || name.length <= 2) {
valid = '<p>Seu nome' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Seu email' + required +'</p>';
}
if(pass = '' || pass.length < 6)
{
valid += '<p>Sua senha' + required + '</p>';
}
if(cpass = '' || cpass.length < 6)
{
valid += '<p>Sua csenha' + required + '</p>';
}
if( cpass != pass)
{
valid += '<p>As senha não são iguais' + required + '</p>';
}
if (message = '' || message.length <= 5) {
valid += '<p>A mensagem' + required + '</p>';
}
if (honeypot != 'http://') {
valid += '<p>Spambots não são permitidos.</p>';
}
if (humancheck != '') {
valid += '<p>Um usuário humano' + required + '</p>';
}
// let the user know if there are erros with the form
if (valid != '') {
$('#wrapper #response').removeClass().addClass('error')
.html('<strong>Por favor, corrija os erros abaixo.</strong>' +valid).fadeIn('fast');
}
// let the user know something is happening behind the scenes
// serialize the form data and send to our ajax function
else {
$('#wrapper #response').removeClass().addClass('processing').html('processamento...').fadeIn('fast');
var formData = $('form').serialize();
submitForm(formData);
}
});
});
Já fiz dois tipo de comparação assim:
if( cpass != pass)
{
valid += '<p>As senha não são iguais' + required + '</p>';
}
e assim:
if( cpass !== pass)
{
valid += '<p>As senha não são iguais' + required + '</p>';
}
mas não vai!
Valeu!
Atenciosamente;
Somente local! Vou colocar online e volto.
@Lucas Guima realmente este tópico está duplicado, devido alguns bugs do próprio site hoje, tentei enviar 2x e deu erro, achei que nem tinha enviado.
Olá como meu problema só ta no front segue o code completo
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX form tutorial using jQuery and PHP</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="ajax_submit.js"></script>
</head>
<body>
<div id="wrapper">
<h1>Give us your feedback</h1>
<div id="inner-wrapper">
<form id="feedback" action="feedback.php" enctype="multipart/form-data" method="post">
<div id="response"><!--This will hold our error messages and the response from the server. --></div>
<div class="inputs">
<label>Name </label>
<input name="name" type="text" class="required" id="name" size="30" />
</div>
<div class="inputs">
<label>Email </label>
<input name="email" type="text" class="required" id="email" size="30" />
</div>
<div class="inputs">
<label>Senha </label>
<input name="pass1" type="text" class="required" id="pass1" size="30" />
</div>
<div class="inputs">
<label>Confirme Senha </label>
<input name="pass2" type="text" class="required" id="cpass2" size="30" />
</div>
<div class="inputs">
<label>Message</label>
<textarea name="message" cols="25" rows="" class="required" id="message"></textarea>
</div>
<div class="button">
<input type="submit" name="submit" id="submit" value="Submit" />
</div>
<div class="inputs">
<input type="hidden" name="honeypot" id="honeypot" value="http://" />
<input type="hidden" name="humancheck" id="humancheck" class="clear" value="" />
</div>
</form>
</div><!-- End inner-wrapper -->
</div><!-- End wrapper -->
</body>
</html>$(document).ready(function() {
$('form #response').hide();
$('#submit').click(function(e) {
// prevent forms default action until
// error check has been performed
e.preventDefault();
// grab form field values
var valid = '';
var required = ' é necessário.';
var name = $('form #name').val();
var email = $('form #email').val();
var pass1 = $('form #pass1').val();
var pass2 = $('form #pass2').val();
var message = $('form #message').val();
var honeypot = $('form #honeypot').val();
var humancheck = $('form #humancheck').val();
// perform error checking
if (name = '' || name.length <= 2) {
valid = '<p>Seu nome' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Seu email' + required +'</p>';
}
if(pass1 = '' || pass1.length < 6)
{
valid += '<p>Sua senha' + required + '</p>';
}
if(pass1 != pass2)
{
valid += '<p>As senha não são iguais' + required + '</p>';
}
if (message = '' || message.length <= 5) {
valid += '<p>A mensagem' + required + '</p>';
}
if (honeypot != 'http://') {
valid += '<p>Spambots não são permitidos.</p>';
}
if (humancheck != '') {
valid += '<p>Um usuário humano' + required + '</p>';
}
// let the user know if there are erros with the form
if (valid != '') {
$('#wrapper #response').removeClass().addClass('error')
.html('<strong>Por favor, corrija os erros abaixo.</strong>' +valid).fadeIn('fast');
}
// let the user know something is happening behind the scenes
// serialize the form data and send to our ajax function
else {
$('#wrapper #response').removeClass().addClass('processing').html('processamento...').fadeIn('fast');
var formData = $('form').serialize();
submitForm(formData);
}
});
});
// make our ajax request to the server
function submitForm(formData) {
$.ajax({
type: 'POST',
url: 'feedback.php',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {
$('#wrapper #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).fadeIn('fast');
if ($('#wrapper #response').hasClass('success')) {
setTimeout("$('#wrapper #response').fadeOut('fast')", 5000);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('#wrapper #response').removeClass().addClass('error')
.html('<p>Houve<strong> ' + errorThrown +
'</strong> erro devido a um<strong> ' + textStatus +
'</strong> condição.</p>').fadeIn('fast');
},
complete: function(XMLHttpRequest, status) {
$('form')[0].reset();
}
});
};Dei uma olhada rapida por cima.... só uma dica ... isso " == " é comparação, e isso " = " é atribuição..
por exemplo nesse trecho de codigo:
if(pass1 = '' || pass1.length < 6)
vc esta atribuindo valor vazio a pass1
substitua da seguinte maneira:
if(pass1 == '' || pass1.length < 6)
e faz um teste
O seu input é type submit, então não use click no botão. Mas sim submit no form
$('#submit').click(function(e) {troque por:$('#feedback').submit(function(e) {
além disso, note que isso é completamente desnecessário:
var name = $('form #name').val();visto que ID é único, não faz sentido deixar o seletor mais lento, informando um pai para ele.
var name = $('#name').val();Valeu galera resolvido, cada dica foi importante então um ponto para cada! :clap:
Abs.
Tem alguma versão no ar?