Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have 10 Textboxes, I need to compare each Textbox value with all other Textboxes using jquery, please help me out with sample example.

JavaScript
var ids = $('input[id*="txtDate"]');
 var i = 0;
 for (i = 0; i < ids.length; i++) {
 // //debugger; 
  if (ids[i].type == 'text') 
  {
   if (ids[i].value == ids[i+1].value)
    alert("plz select other date");
    return false;
  }
 } 
I am trying to use the above code which i have mentioned, but the loop is not working properly
Posted
Updated 2-Jan-13 0:06am
v2

Some changes to your code -
JavaScript
var ids = $('input[id*="txtDate"]');
var i = 0, j = 0;
for (i = 0; i < ids.length - 1; i++) {
// //debugger;
 if (ids[i].type == 'text')
 {
   for (j = i+1; j < ids.length; j++) {
     if (ids[i].value == ids[j].value) {
      alert("plz select other date");
      ids[j].focus();
      return false;
     }
   }
 }
}

Hope it helps you out...

Regards,
Niral Soni
 
Share this answer
 
I would look to set up a double nested for each loop using jquery;
(assuming your inputs are named txtDate1,2,3,4 etc.

JavaScript
//find all inputs with ids beginning with "txtDate" and of type="text"
$('input[id^="txtDate"][type="text"]').each(
  //start outerloop against each element
  function (index, element) {
    //find all the items again
    $('input[id^="txtDate"][type="text"]').each(
      //start inner loop on each item
      function(indexInner, elementInner) {
        if (index != indexInner)
        {
          //compare the items here as they are not the same index numbers so are different elements
          //there are different ways to compare, but you could use jquery again for this
          //and use the element and elementInner to access each of them.
            if ( $(element).val() == $(elementInner).val())
               alert("2 boxes have the same value:" +element.id + ":" + elementInner.id);
        }
     }
   );
  }
);

If a match was found I would also return false out of the comparison check to stop checking any further.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900