3

I have a html table and there is input in it. I want to check duplicate value in a custom column or In the other hand Non Dupllicated td value.

<table>
    <tr><td><input value="one"/></td></tr>
    <tr><td><input value="two"/></td></tr>
    <tr><td><input value="one"/></td></tr>
    <tr><td><input value="nine"/></td></tr>
    <tr><td><input value="four"/></td></tr>
</table>

I want check all input in td and if a value duplicated display error. For example in top code one is duplicated. I don't want to use for().

1

4 Answers 4

18

Using each() check value of inputs and if any value is duplicate add class duplicate to it.

var arr = [];
$("input").each(function(){
    var value = $(this).val();
    if (arr.indexOf(value) == -1)
        arr.push(value);
    else
        $(this).addClass("duplicate");
});
.duplicate {
    border: 1px solid red;
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr><td><input value="one"></td></tr>
    <tr><td><input value="two"></td></tr>
    <tr><td><input value="one"></td></tr>
    <tr><td><input value="nine"></td></tr>
    <tr><td><input value="four"></td></tr>
    <tr><td><input value="four"></td></tr>
</table>

Sign up to request clarification or add additional context in comments.

3 Comments

I don't want make a distinct table ,I want that exist duplicate value has error
@MohsenZahedi Write your opinion about my answer.
@MohsenZahedi What is meaning of not work? In snippet or in your local?
4

Try this one

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
        <tr><td><input value="one"></td></tr>
        <tr><td><input value="two"></td></tr>
        <tr><td><input value="one"></td></tr>
        <tr><td><input value="nine"></td></tr>
        <tr><td><input value="four"></td></tr>
    </table>

<script>
$("input").change(function(){
    var x=$(this).val();
    var z=0;
    $("input").each(function(){
        var y=$(this).val();
        if(x==y){
            z=z+1;
        }
    });
    if(z>1){
        alert(x);
    }
 })
 </script>

 </body>
</html>

Comments

0

Please check using this code

$("input").on("change",function(){
   var arr = new Array();
   $("input").each(function(){
                        arr.push($(this).val());
   });
   for(var i=0; i<arr.length;i++){
       for(var j=i+1;j<arr.length;j++){
          if(arr[i]==arr[j]){
              alert("Already Exist"); 
              return;
          }
       }
   }         
})

1 Comment

It will be bit helpful if you can give small description about you approach.
0

I used JQuery to check two columns on duplicates, starting off by hiding rows that do not contain:

     $("#webGrid tr > td.TrailerNumber:not(:contains('" + CurrentTrailerNumber 
 + 
 "'))").parent().hide(); //Hide rows on TrailerNumber that don't match.
            $("#webGrid tr > td #CarrierNameLbl:not(:contains('" + 
 CurrentCarrrierNameLbl + "'))").parent().closest('TR').hide(); //Hide Rows 
 that don't contain value.

You can also test the length of strings to validate:

 //Test lengths to determine if an exact match. 
            if ($('#webGrid tbody > tr > td #TrailerNumber').val().length > TrailerNumberLength || $('#webGrid tbody > tr > td #TrailerNumber').val().length != TrailerNumberLength) {
                $('#webGrid TR').parent().closest('TR').hide();
            };

            if ($('#webGrid tbody > tr > td #CarrierName').val().length > CurrentCarrierNameLength || $('#webGrid tbody > tr > td #CarrierName').val().length != CurrentCarrierNameLength) {
                $('#webGrid TR').parent().closest('TR').hide();
            };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.