1

I have seen similar issues on this site but I can't get this to work. I am trying to detect duplicate entries in input boxes through Javascript - but I want the loop to break when a duplicate is entered. I have that part working, but the loop continues to run and it creates an infinite loop that the user can't get out of. I am trying to break the loop and have the user re-enter a different value.

function checkDuplicates() {
        var numFlds = <cfoutput>#form.UnitCount#</cfoutput>;
            for (var x=1; x<=numFlds; x++) {
                for (var y=x+1; y<=numFlds; y++) {
                    if (document.getElementById('SN'+y).value !== '') 
                        if (document.getElementById('SN'+x).value == document.getElementById('SN'+y).value) {
                            alert('Duplicate Serial Number Entered!');
                                break;
                            }
                        }
                    }
                }
  • 4
    which loop want you to exit? – Nina Scholz Sep 19 '17 at 14:22
  • 4
    If you want to break out of all the loops in your code, you can just `return;` – George Sep 19 '17 at 14:22
  • Possible duplicate of [**How to stop a JavaScript for loop?**](https://stackoverflow.com/questions/9830650/how-to-stop-a-javascript-for-loop) - or [**Best way to break from nested loops in Javascript?**](https://stackoverflow.com/questions/183161/best-way-to-break-from-nested-loops-in-javascript) – Nope Sep 19 '17 at 14:23
  • 3
    Possible duplicate of [Best way to break from nested loops in Javascript?](https://stackoverflow.com/questions/183161/best-way-to-break-from-nested-loops-in-javascript) – Roy Berris Sep 19 '17 at 14:27
  • Thanks for the quick responses! I want to exit all loops and let the user enter the correct value. Using "return;" makes the alert box continue to pop up. I am stuck at the alert box continuing to pop up and not break out of the loop. – stevewlindsay Sep 19 '17 at 14:35

2 Answers2

2

you could declare a bool duplicate found and make a check on every loop for it. declare the duplicate = true when one is found.

for(x=0; x < Num && duplicate ; x++)

and when you find one, the loop stop, the next one stopps too, and the next one too. and you're out.

if you want to remember where you left off, you could save the corresponding x to save where you left off.

lg!

Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22
1

There is a way to jump out to a label reference. For the next example, you have three labels (label1, label2, label3). If you use 'break label3' automaticaly your code go to the end (or to the label you desired)

var numFlds = <cfoutput>#form.UnitCount#</cfoutput>;
label1:
for (var x=1; x<=numFlds; x++) {
    label2:
    for (var y=x+1; y<=numFlds; y++) {
        if (document.getElementById('SN'+y).value !== '') {
            if (document.getElementById('SN'+x).value == document.getElementById('SN'+y).value) {
                alert('Duplicate Serial Number Entered!');
                break label3;
            }
        }
    }
}
label3:
console.log('Finish!');
Newton
  • 47
  • 9