Shermaine Chaingan Shermaine Chaingan - 3 months ago 11
Javascript Question

How will i prevent duplication of value and empty value

How will i prevent duplication value that added in combobox and prevent also the space value ? i used this code but its not working the duplication of value when i insert the value its working fine at first input but in second input its alerting that i have duplicate valuebut i entered different value any help thank you in advance

<HTML>
<HEAD>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<fieldset>
<legend>Combo box</legend>
Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/>
Selected: <input type="text" name="selected" id="selected"/>
IMEI Selected: <input type="text" name="imei" id="imei"/>

<input type="button" id="button" value="Add" onclick="addCombo()">
<br/>
Combobox: <select name="combo" multiple id="combo"></select>
</fieldset>
</BODY>
</HTML>
<script>

$("#txtCombo").on("keydown", function (e) {
return e.which !== 32;
});


$(document).ready(function() {

$('#button').click(function(){

var data = [];
$.each($("#combo option:selected"), function() {
data.push($(this).attr("value"));

});
$('#imei').val(data.join(","));;
var count = $("#combo :selected").length;
$('#selected').val(count);


});

});


$("#combo").on('change', function () {

var count = $("#combo :selected").length;
$('#selected').val(count);


});
var text = $("#text").val();


function addCombo() {

var textb = document.getElementById("txtCombo");
var combo = document.getElementById("combo");
var option = document.createElement("option");
option.text = textb.value;
option.value = textb.value;
option.selected = true;
if (textb.length == 0) {
return false;
}
if (combo.length) {
alert("Duplicate found");
return false;
}
try {
combo.add(option, null ); //Standard
}catch(error) {
combo.add(option); // IE only
}
textb.value = "";
}


// separated by comma to textbox
$(document).ready(function() {

$("#combo").change(function() {
var data = [];
$.each($("#combo option:selected"), function() {
data.push($(this).attr("value"));
});
$('#imei').val(data.join(","));;

});

});
</script>

Answer

To find the duplicate you can use following function(using jQuery)

function isDuplicate(value,text){
/*Get text of the option identified by given value form the combobox and then check if its text matches the given text*/
    if($('#combo select option[value="' + value + '"]').text() == text)
        return true;
    else 
        return false;
}

Update:

function addCombo() {

    var textb = document.getElementById("txtCombo");
    var combo = document.getElementById("combo");
    var option = document.createElement("option");
    var value = textb.value.trim();
    option.text = value;
    option.value = value;
    option.selected = true;
    if (textb.length == 0) {
        return false;
    }
    if ($('#combo option[value="' + value + '"]').text() == value ) {
        alert("Duplicate found");
        return false;
    }
    try {
        combo.add(option, null ); //Standard 
    }catch(error) {
        combo.add(option); // IE only
    }
    textb.value = "";
}
Comments