-2

I have n number of textboxes on a form. After the user enters a value in a textbox, I need to check that it's not a duplicate of any of the other textboxes. For example:

Textbox[0] : 1
Textbox[1] : 2 
Textbox[2] : 3
Textbox[4] : 1

It should alert saying that '1' has been entered twice.

I tried the following:

function findDuplicates ( obj ) {
    var inputs = document.getElementsByName('analog_idorder[]');
    var answers= new Array();

    for(var ii = 0; ii < inputs.length; ii++) {
        if(inputs[ii].type == 'text') {
            for(var jj = 0; jj < answers.length; jj++) {
                if(answers[jj] == inputs[ii].value)
                    alert('Duplicate Id_order number');
                return false;
            }
            answers.push(inputs[ii].value);
        }
    }
    return true;
}

But only the first two textboxes validate.

Let me know how I can solve this problem.

3
  • Have you made sure that the name, analog_idorder[], is actually the same for all textboxes? Try alert(inputs.length); on line 2, to see how many textboxes it found. Also, if you've, say, spelled 'text' wrong, as the type parameter, they would still show up as textboxes, but wouldn't meet your first criteria. try alert(ii) after the first 'if' as well. Commented Jul 14, 2009 at 6:39
  • duplicate: stackoverflow.com/questions/1119495/… Commented Jul 14, 2009 at 6:48
  • Ugh, I guess the first 4 answers were too....? Commented Jul 14, 2009 at 6:51

3 Answers 3

1

Try adding brackets for the if statement in the nestled loop:

            for(var jj = 0; jj < answers.length; jj++){
                if(answers[jj] == inputs[ii].value){
                   alert('Duplicate Id_order number');
                   return false;
                }

            }

Else the loop will always return false at the first iteration.

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

1 Comment

Thanks Baversjo i Will try out and let you know
1

You could get rid of all the looping you are doing by using a associative array to store the values you've seen:

var answers = document.getElementsByName("analog_iorder[]");
var seen = {};

function checkForDupes() {
  for (var i=0;i<answers.length;i++) { 
    if (seen[ answers[i].value ]) { 
      alert("Duplicate "); 
      return false;
    } 
    seen[ answers[i].value ] = true; 
  }
  return true;
}

Comments

1
Text 1: <input type="text" id="txt1" onblur="chkValue('txt1');" /><br/>
Text 2: <input type="text" id="txt2" onblur="chkValue('txt2');" /><br/>
Text 3: <input type="text" id="txt3" onblur="chkValue('txt3');" /><br/>
Text 4: <input type="text" id="txt4" onblur="chkValue('txt4');" /><br/>
Text 5: <input type="text" id="txt5" onblur="chkValue('txt5');" /><br/>
Text 6: <input type="text" id="txt6" onblur="chkValue('txt6');" /><br/>
Text 7: <input type="text" id="txt7" onblur="chkValue('txt7');" /><br/>
Text 8: <input type="text" id="txt8" onblur="chkValue('txt8');" /><br/>
Text 9: <input type="text" id="txt9" onblur="chkValue('txt9');" /><br/>
Text 10: <input type="text" id="txt10" onblur="chkValue('txt10');" /><br/>

<script type="text/javascript">
var vals=new Object();

function chkValue(a)
{
    var t=document.getElementById(a);

    if(vals[t.value])
    {
        if(vals[t.value] != a)
        {
            alert("You have already entered ["+t.value+"]");
            t.value="";
        }
    }
    else
    {
        vals[t.value] = a;
    }
}

</script>

Comments

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.