Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have dynamic text boxes. how to validate the dynamic textboxes so that no two textboxes can contain same value. I have written validation for checking empty check box is as follows
JavaScript
for (var i = 0; i <= 20; i++)
        {
            var id = "txt" + i
            var txtbox = document.getElementById(id)
            if (txtbox) {

                if (txtbox.value != null && txtbox.value.trim() == "") {

                    alert("Serial Number field cannot be empty");

                    return false;

                }

            }
Posted

Logic



  • Store all TextBox values in an Array
  • Sort the Array
  • Compare Consecutive values

Demo


[Demo] Check if any two TextBox have same value[^]
JavaScript
var allTextBoxes = [];

$('input[type=text]').each(function () {
    allTextBoxes.push($(this).val())
});

var sorted_arr = allTextBoxes.sort();

for (var i = 0; i < allTextBoxes.length - 1; i++) {
    if (sorted_arr[i + 1] == sorted_arr[i]) {
        alert("Please enter different value in each TextBox.");
        return false;
    }
}
 
Share this answer
 

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