Raviteja
Raviteja

Reputation: 3479

How to show alert for duplicated value of textbox

I have multiple input fields with different values. I can edit them but cannot be empty (if empty show an alert). When I am editing it to new values, if the new value matches with any of the other inputs values, I need an alert that you cannot use this.

HTML

<input type="text" class="selector" value="new">
<input type="text" class="selector" value="old">
<input type="text" class="selector" value="newest">
<input type="text" class="selector" value="oldest">
<input type="text" class="selector" value="older">

JavaScript

$('.selector').change(function () {
    alert();
});

Fiddle here

Upvotes: 6

Views: 10409

Answers (4)

madalinivascu
madalinivascu

Reputation: 32354

Try this Code:

$('.selector').on('blur',function () {
   if ($('.selector[value="'+$(this).val()+'"]').not($(this)).length > 0 || current_value.length == 0 ) {
    alert('duplicate value');
   }
});

http://jsfiddle.net/0c0qep6y/12/

Upvotes: 2

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Updated Fiddle.

You can refresh the value attribute using $(this).attr('value', $(this).val()); first, then check if there's any other field with class selector has the same value using value selector $('.selector[value="' + current_value + '"]').length.

Hope this helps.


$('body').on('blur', '.selector', function () {
    $(this).attr('value', $(this).val());

    if ($('.selector[value="' + $(this).val() + '"]').length > 1 ) {
        $(this).focus();
        alert('You cannot use this');
    }else if($(this).val().length == 0) {
        alert('Empty value not accepted');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="selector" value="new">
<input type="text" class="selector" value="old">
<input type="text" class="selector" value="newest">
<input type="text" class="selector" value="oldest">
<input type="text" class="selector" value="older">

Upvotes: 7

HGrover
HGrover

Reputation: 225

$('.selector').change(function () {
  var changedItem = $(this);
  var otherSelectors = $(".selector").not(changedItem);
  var matchingItemPresent = [];
  $.each(otherSelectors, function (count, item) {
    $(item).val().trim()== changedItem.val().trim();
    matchingItemPresent.push($(item));
  });
  
  matchingItemPresent.length > 0 ? alert("Duplicate entry..same value exists in another field") : $.noop();
  
  matchingItemPresent[0].focus();
  console.log(matchingItemPresent[0]);
});

updated to focus on the text box JS Updated

Upvotes: 0

Waqar Ahmed Khan
Waqar Ahmed Khan

Reputation: 21

Use unique Id's for each input text field and get the values of them in your JavaScript method using getElementById("[YourDefinedId]") and compare them with each other and show alter.

HTML

<input type="text" class="selector" id="first" value="new">
<input type="text" class="selector" id="second" value="old">
<input type="text" class="selector" id="third" value="newest">
<input type="text" class="selector" id="fourth" value="oldest">
<input type="text" class="selector" id="fifth" value="older">

JavaScript

function compareValues(){

var first = getElementById("first").value;
var second = getElementById("second").value;
var third = getElementById("third").value; 
var fourth = getElementById("fourth").value;
var fifth = getElementById("fifth").value;

//Now you can compare them using null check and empty.

}

Upvotes: 0

Related Questions