how to compare two text input type box values using jquery

I have a two textboxes t1 and t2 . I would like to figure out how in jquery if the values in t1 and t2 are same I can display an alert message. If user does not change the values I want to prevent the form from being submitted.


Try something like this

$('form').submit(function(evt) {
   if ($('#textbox1').val() === $('#textbox2').val()) {
     alert('values match');

uses the .submit() method to bind to the submit event for the form, then compares the 2 values from the textboxes - if they are the same it displays an alert then prevents the default action (form submission) using event.preventDefault()

Working example here

$("form").submit(function() {
  var _txt1 = $('#txt1').val();
  var _txt2 = $('#txt2').val();
  if (_txt1 == _txt2)
     return true;
    alert('Not matching!');
    return false;
<script src=""></script>
<form action="#" method="post" name="form_name" id="form_id">
  <input type="text" id="txt1" name="txt1" />
  <input type="text" id="txt2" name="txt2"/>
  <input type="submit" value="Submit">


$(document).ready(function() {
    var _t1 = $("#t1");
    var _t2 = $("#t2");
    $(_t2).focusout(function() {
    if(_t1.val() === _t2.val()){
        return alert('Match');
    return alert('No match');


<input id="t1">
<input id="t2">


Here is the code

if(!$("#t1").val() && !$("#t2").val() && $("#t1").val() === $("#t2").val()){
   alert("Both values are same");
   return false;

I am doing strict type check for input values by ===.

Hope this will help your requirement.

Need Your Help

TreeSet acting weird

java collections edt

I'm having a weird problem with TreeSet (sortedNodes) and ArrayList (nodes). In my program, I have in a method called from Event Dispatch Thread (from ActionListener) these lines: