Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET jQuery
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.

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 1-Jan-13 23:52pm
Edited 2-Jan-13 0:06am
DaveAuld174.6K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Some changes to your code -
 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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I would look to set up a double nested for each loop using jquery;
(assuming your inputs are named txtDate1,2,3,4 etc.
 
//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.
  Permalink  
v4

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 395
1 Maciej Los 268
2 OriginalGriff 230
3 CHill60 221
4 Sibeesh KV 139


Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100