Check if phone field is the same

0

Someone needs help from you.

I need a validation that is as follows.

In my form I have two phone fields, TEL1 and TEL2.

Validation is as follows if TEL2 == TEL1 Gives an alert that the number has already been reported.

Can anyone help me?

JS:

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


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

    if (telefone2 === telefone1) {
        console.log("teste");
    }
}
    
asked by anonymous 16.03.2018 / 17:39

3 answers

0

Using your example ...

$('[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><inputname="telc1" />
<input name="telc2" />
    
16.03.2018 / 18:02
1

First, this is wrong:

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

Should be:

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

You can check using the 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><inputtype="text" name="telc1" value="">
    <input type="text" name="telc2" value="">
    <button>Enviar</button>
</form>
    
16.03.2018 / 18:02
0

Get the value of each input and compare them as follows:

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!');
    }
  });
});
    
16.03.2018 / 17:48