0

Pessoa preciso de uma ajuda de vocês.

Eu preciso de uma validação que é a seguinte.

Em meu form eu tenho dois campos de telefone, TEL1 e TEL2.

A validação é a seguinte se TEL2 == TEL1 Da uma alerta falando que o numero já foi informado.

Alguém pode me ajudar?

JS:

$('[name="telc2"]').change(function () {
    ValidacaoTelefone();
});


function ValidacaoTelefone() {
    var telefone1 = ('[name="telc1"]').val();
    var telefone2 = ('[name="telc2"]').val();

    if (telefone2 === telefone1) {
        console.log("teste");
    }
}
3
  • informe seu código para ajudarmos, se tiver algo errado iremos orientar. 16/03/2018 às 16:49
  • Qual sua dúvida? A pergunta parece uma encomenda de código... Não tem nenhum que sirva de base? 16/03/2018 às 16:52
  • editei o topoico com o codigo 16/03/2018 às 16:55

3 Respostas 3

1

Primeiro, isto esta errado:

var telefone1 = ('[name="telc1"]').val();
var telefone2 = ('[name="telc2"]').val();

Deveria ser:

var telefone1 = $('[name="telc1"]').val();
var telefone2 = $('[name="telc2"]').val();

Pode verificar usando o evento onblur:

$(document).ready(function() {
  var telefone1 = $("[name=telc1]"),
      telefone2 = $("[name=telc2]");
  
  $("[name=tel1], [name=tel2]").on('blur', function() {
    var v1 = telefone1.val(),
        v2 = telefone2.val();

    if(v1 == v2) {
      alert('Numero ja informado!');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
    <input type="text" name="telc1" value="">
    <input type="text" name="telc2" value="">
    <button>Enviar</button>
</form>

0

Usando seu exemplo...

$('[name="telc2"]').blur(function () {
    ValidacaoTelefone();
});


function ValidacaoTelefone() {
    var telefone1 = $('[name="telc1"]').val();
    var telefone2 = $('[name="telc2"]').val();

    if (telefone2 != telefone1) {
        console.log("Diferente");
        return;
    }
    
    console.log("Igual");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="telc1" />
<input name="telc2" />

0

Pega o valor de cada input e compara eles da seguinte forma:

HTML

<form method="post" action="" id="form">
    <input type="text" name="tel1" id="tel1" />
    <input type="text" name="tel2" id="tel2" />
    <button>Enviar</button>
</form>

JS

$(document).ready(function() {
  $('#tel1, #tel2').blur(function() {
    var tel1 = ('#tel1').val();
    var tel2 = ('#tel2').val();

    if(tel1 == tel2) {
      alert('Numero ja informado!');
    }
  });
});
2
  • Teria como fazer de uma outra forma onde ele verifica assim que troca de campo? tipo digitei 1111 no tel1 ai eu digito 1111 no tel2, quando eu troco para outro campo ele verifica 16/03/2018 às 16:53
  • Alterei minha resposta. 16/03/2018 às 17:03

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .