Question

I am developing a application in MVC. I want to find if array contains the values selected by user from drop down list. I have a code -

<div class="span10" style="margin-left:0px;">
    <a href="#" id="lnkAddProduct" style="font-size:14px;text-decoration:none;margin-right:10px;margin-left:30px;">Add Product</a>
    <span id="LinkErrorMsg" class="field-validation-error"></span>
</div>

and my jQuery code -

<script type="text/javascript">
$(document).ready(function () {         
    $('#lnkAddProduct').click(function () {
        var rIndex = $("select.clsProductId").length;       
        var ndate =  new Date();    
        var time = ndate.getMilliseconds();
        var IDD = rIndex + time;                           
        $('#ProductList').append("<div  class='span12' style='margin-left:0px' ><div class='span3'><select class='clsProductId '  name='ProductId' id='ddProductList_"+IDD+"' style = 'font-size:12px;width:200px;margin-right:10px;margin-left:20px;' onchange='get("+IDD+")'/> </div><span id='ProductIdValidation field-validation-error'></span><div id='ProductCode_"+IDD+"' class='span1'  style=' margin-left:30px;'></div><div id='Weight_"+IDD+"' class='span1' style=' margin-left:90px;'> </div><input type='text' id='Quantity_"+IDD+"'  name='Quantity' style='width:50px; margin-left:35px;' /> <a href='#' style='font-size:14px;text-decoration:none;font-weight:bold; margin-left:20px' id='lnkRemove_"+IDD+"' class='clsRemove'  onclick='removeElement(" + IDD+");'>X</a></div>");                
        getProductList(IDD);
    });        
});
function get(Index)
{  
    var productCode=$('#ddProductList_' + Index).val();  
    getProductDetails(productCode,Index)
}
function getProductDetails(productCode,Index)
{
    $.ajax({
        url: "/Product/getProductDetails/",
        data: { Id: productCode },             
        dataType: "json",
        success: function (result)
        {                           
            $('#ProductCode_'+Index).text(result.ProductCode);         
            $('#ProductName_'+Index).text(result.ProductName);           
            $('#Weight_'+Index).text(result.Weight);    
        }
    });
    CheckProductReapet();
}  
function CheckProductReapet() {
    var Count = document.getElementsByName("ProductId");
    var arrAptNumbers = new Array();
    for (var i = 0; i < Count.length; i++) {
        var s = Count[i].id;
        var productCode = $(s).val();
        var stringsplit = Count[i].id;
        var s = stringsplit.split("_")
        var Id = s[1];
        var nQuantity = $('#ddProductList_' + Id).val();                
        alert('nQuantity-'+nQuantity);
        if (arrAptNumbers.contains(nQuantity)) {
            $('#ProductIdValidation').text("Product Name should not be reapeted.")
        }
        else {
            arrAptNumbers.push(values[i].value);
            $('#ProductIdValidation').text("");
        }
    }
}     
</script>

Here,I want to check if arrAptNumbers contains a value of #ddProductList_ ,so I give a code as -

if (arrAptNumbers.contains(nQuantity)) {
                $('#ProductIdValidation').text("Product Name should not be reapeted.")
            }

but,it will not show a method as contains for arrAptNumbers. How to take that? Any solution?

Was it helpful?

Solution

You can use .indexOf() to check that:

if(arrAptNumbers.indexOf(nQuantity) > -1){
    $('#ProductIdValidation').text("Product Name should not be reapeted.")
}

OTHER TIPS

You can use $.inArray()

if($.inArray(nQuantity,arrAptNumbers)) > -1)
    //value exists
};

OR

You can use indexOf(), it returns the first index at which a given element can be found in the array, or -1 if it is not present.

if (arrAptNumbers.indexOf(nQuantity) > -1){
    //value exists
};

Note: indexOf() is supported in IE > 8

if ($.inArray(nQuantity, arrAptNumbers) > -1){
 $('#ProductIdValidation').text("Product Name should not be reapeted.")
}

For further reference please see the below link.

How to find if an array contains a specific string in JavaScript/jQuery?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top