Check if phone field is equal

Asked

Viewed 51 times

0

Person I need a help from you.

I need a validation which is as follows.

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

Validation is as follows if TEL2 == TEL1.

Can someone 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");
    }
}
  • inform your code to help, if there is something wrong we will guide.

  • What is your question? The question seems like a code order... There is none that serves as a basis?

  • edited the top with the code

3 answers

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 event 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

Take 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!');
    }
  });
});
  • Would there be any other way where he checks so that field change? type typed 1111 in tel1 ai I type 1111 in tel2, when I change to another field it checks

  • I changed my answer.

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>
<input name="telc1" />
<input name="telc2" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.