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;
                            }
                        }
                    }
                }
5
  • 4
    which loop want you to exit? Commented Sep 19, 2017 at 14:22
  • 4
    If you want to break out of all the loops in your code, you can just return; Commented Sep 19, 2017 at 14:22
  • Possible duplicate of How to stop a JavaScript for loop? - or Best way to break from nested loops in Javascript? Commented Sep 19, 2017 at 14:23
  • 3
    Possible duplicate of Best way to break from nested loops in Javascript? Commented Sep 19, 2017 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. Commented Sep 19, 2017 at 14:35

2 Answers 2

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!

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

Comments

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!');

1 Comment

For some reason this code no longer detects duplicate field values?

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.