Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check duplicate entries in a form 1

Status
Not open for further replies.

nonprogrammer

Technical User
Dec 28, 2005
143
0
0
US
Hello all,

I have an asp form that has a field where you can input numbers. I was wondering if there is a way to add a javascript that will check for duplicate numbers. For example if the user enters:
Code:
UserName  PartNumber  StockNumber
Sam         123ABC       1
Sam         123ABC       2
Sam         234DEF       1

What I want to avoid is the user entering the same number in the StockNumber field.

Any help is greatly appreciated
 
You could do this a number of ways.
You could have an onchange or even an onblur event for each StockNumber field call a function. In that function you compare the value of the field that called the function with all of the other similar fields looking for a match.

Or, you create an array and have an event fire each time they enter a value in a stocknumber field, check the array to see if the value is already in use and if not, add the new value to the array. If the value is found in the array already then display and error message and set focus back to the field so they can change it.

Or, you use onsubmit to call your function prior to submitting the page, compare the field values and return false if there was a duplicate so the form will not submit.

These are just a few possible approaches, the last one is probably best.
How many fields will you have to compare on the form?
Is it a fixed number or might it vary?
If you had only three fields you could always do it simply just by writing statements to compare all the fields against each other.
If the number of fields vary or there are a lot of them then you would be better off creating a short function that looped through all the field values making the compare rather than creating individual logic statements to do each compare. But it all depends on how you are using them.

Give each of the fields a sequential ID like Field1, Field2, Field3 so that you can easily loop through the whole set of them to do your compares.



At my age I still learn something new every day, but I forget two others.
 
theniteowl, thanks so very much for the response, that helps. But I the form is dynamic and to be honest I am not sure how to write the whole javascript could you help me get started?

in advance thanks
 
This is a quick example.
All of the fields are given the same name so when I check document.myform.MyField.length it gives me the number of fields with that name that I have to loop through.
I set up nested for loops. The outer loop is used to control which field I am testing for matches against, the inner loop goes through all the rest of the fields.
The inner loop does not start at the beginning because you do not want to compare the first field against itself so it always starts 1 higher. It also helps to prevent unnecessary compares like when x gets to 3, it does not have to compare against 1 and 2 because earlier in the loop you already compared 1 and 3 so no need to do it again.

I did this quick and it seems to work but try it with a larger number of fields to see how it holds up.

Code:
<html>
<head>
<script type="text/javascript">
function testDupes() {
  var numFlds = document.myform.MyField.length;
  for (var x=0; x<numFlds; x++) {
    for (var y=x+1; y<numFlds; y++) {
      if (document.myform.MyField[x].value == document.myform.MyField[y].value) {
        alert('There was a match');
        return false;
      }
    }
  }
  alert('No matches');
  return true;
}
</script>
</head>
<body>
<form name="myform" method="post" action="myfile.asp" onsubmit="return testDupes();">
Field 1: <input type="text" id="MyField"><br>
Field 2: <input type="text" id="MyField"><br>
Field 3: <input type="text" id="MyField"><br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>

At my age I still learn something new every day, but I forget two others.
 
thank you so very much that has help tremendously!!! theniteowl you are the best!!
 
Ohh sorry one more question,
what if "Myfield" is "Myfield1","Myfield2","Myfield3
 
That would require a different method for looping through them.
With them all named the same they constitute an array named MyField of which we already know the length.
If you have them named sequentially like MyField1, MyField2, etc, then you have to know how many of the fields are on the page.

If you are creating the fields dynamically then the script would know how many fields it has created. You would save that value in a variable for Javascript to use and modify the function to the code below. Note that you will need to supply the value for numFlds.

Code:
function testDupes() {
  var numFlds = 7;
  for (var x=1; x<=numFlds; x++) {
    for (var y=x+1; y<=numFlds; y++) {
      if (document.getElementById('MyField'+x).value == document.getElementById('MyField'+y).value) {
        alert('Fields matched!');
        return false;
      }
    }
  }
  alert('No matches');
  return true;
}

At my age I still learn something new every day, but I forget two others.
 
got it.
Now the part that I am a bit confused the
getElementById is the same as myform like in document.myform.MyField?
 
It's kind of the same, but at the same time it's not.

document.getElementById uses an element's id to "search" for the element. Ids must be unique on HTML elements (which means you can't have 2 elements with the same id), and for that reason document.getElementById will always return a reference to 1 and only 1 HTML element.

document.myform.MyField uses an element's name instead of it's id to locate the element. Names do not have to be unique, so multiple elements can have the same name. So, when accessing an element using the document.form.elementName method, it can return an array of element references depending on how many elements share that name. Additionaly, for the way you've supplied, the element must also reside in a form. Using document.getElementById, the element being referenced does not have to be in a form. Now... that doesn't mean that you can't reference an element by it's name if it's not in a form, you just have to use a different method. document.getElement[!]s[/!]ByName will return an array of element references that match the supplied name (and they do not need to reside in a form). Take special note the "s" on the end of Elements (it's plural specifically because elements can share a name, and as such can return an array of information, unlike document.getElementById)

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top