0

If the first and second fields have the same text despite the font and spaces, then delete the second one text

$(document).ready(function() {
  var input_1 = $(".test_1");
  var input_2 = $(".test_2");
  $(input_2).focusout(function() {
    if (input_1.val() === input_2.val()) {
      $('.test_2').val('');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<input value="dom" class="test_1">
<input value=" Dom" class="test_2">
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
G-Force53
  • 13
  • 4
  • What's your question? It seems to work as described. Please see [ask] and take the [tour]. – isherwood Nov 01 '22 at 14:28
  • Your question is effectively, 'how can I compare two strings ignoring case and whitespace?'. The duplicates have the answers. – Rory McCrossan Nov 01 '22 at 14:29
  • 1
    I apologize for my English, how can I compare two strings ignoring case and whitespace? it's more correct this way! – G-Force53 Nov 01 '22 at 14:33

1 Answers1

0

Perhaps you want a trim and a lowerCase?

$(function() {
  const $input_1 = $(".test_1");
  const $input_2 = $(".test_2")
  .on("blur",function() {
    const val1 = $input_1.val().trim().toLowerCase(), 
          val2 = $input_2.val().trim().toLowerCase();
    console.log(val1,val2,val1 === val2);
    if (val1 === val2) {
      $input_2.val('');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<input value="dom" class="test_1">
<input value=" Dom" class="test_2">
mplungjan
  • 169,008
  • 28
  • 173
  • 236