Click to See Complete Forum and Search --> : Form validation question


betheball
12-19-2003, 11:34 AM
I have an order form that allows users to order up to 10 products at a time. Among the fields are Product1, Product2, etc., all the way up to Product10. Is there some sort of javascript I can use that on submit will make sure the same product has not been entered twice? The form has a quantity field next to each product and I want the user to enter 2 widgets, not 1 widget twice. So, I need a script that will scan Product1-Product10 checking for duplicates and return an alert if duplicates are found.

Any ideas?

olerag
12-19-2003, 12:15 PM
You may or may not have to check all 10.

1. Put only values that have a length > 0 into an array.
2. Iterate thru the array and check for duplicate values.
3. Handle errors as you need/desire.

Would you like a code example????

betheball
12-19-2003, 12:49 PM
Pretty please. My ASP is improving, but the extent of my java is using google, finding the code I need and pasting it into my page.

olerag
12-19-2003, 03:16 PM
OK - here's an demo w/ five fields but the quantity doesn't
matter. You can add five more and the array will simply
grow if values are entered. And in this example its OK for
all of the fields to be empty (i.e., NULLs are not dups).

Note that the array fill is based on all of the textfields in the
form so if you have others that should not be included,
perform an additional "if" clause (checking for, oh, maybe a
substring of the object name) between the two "ifs" shown.


<html>
<head>
<script type="text/javascript">
function checkDups(form) {
var dupArray = new Array();
var count = 0;

<!-- Fill the array with fields that have values -->
for (var i=0; i<form.elements.length; i++) {
if (form.elements[i].type == "text") {
if (form.elements[i].value.length > 0) {
dupArray[count] = form.elements[i].value;
count++;
}
}
}

<!-- Check for dups and stop on first error found -->
if (dupArray.length > 1) {
for (var i=0; i<dupArray.length; i++) {
for (var j=0; j<dupArray.length; j++) {
if (i != j) {
if (dupArray[i] == dupArray[j]) {
alert("Values are identical: " + dupArray[i]);
return(false);
}
}
}
}
}
alert("No duplicate values were found.");
return(true);
}
</script>
</head>
<body>
<center>
<b>Duplicate Contents Demo</b>
<form>
<input type="text" name="field1" size="15" length="15">
<input type="text" name="field2" size="15" length="15">
<input type="text" name="field3" size="15" length="15">
<input type="text" name="field4" size="15" length="15">
<input type="text" name="field5" size="15" length="15">
<p>
<input type="button" value="Check Duplicates" onClick="checkDups(this.form)">
</form>
<hr>
</center>
</body>
</html>



Also, the comparison is not case-sensitive. For this, add
toLowerCase() to the test, such as:


if (dupArray[i].toLowerCase() == dupArray[j].toLowerCase())

betheball
12-20-2003, 06:13 PM
How would I edit the above code so it will only check product1, product2, product3, etc.? My other fields are quantity1, quantity2, quantity3, etc. Those fields can have duplicate values.

olerag
12-20-2003, 07:53 PM
That is what I mentioned in my second paragraph with the
demo code. If you only want to check on "text" objects
with a specific name (such as "product1" thru "productx")
place an if clause between the other two "ifs" in the first
"for" iteration. Such as...


if (form.elements[i].type == "text") {
if(form.elements[i].name.substring(0,7).toLowerCase() == "product") {
if (form.elements[i].value.length > 0) {


Now, only textfields with any name that begins with "product"
shall be examined and placed into the array. And don't
forget the closing "brace".

betheball
12-20-2003, 10:06 PM
Thanks so much for the help. Java is still pretty foreign to me, hence the request for additional clarification. Any good books you recommend for learning Javascript?

fredmv
12-20-2003, 10:09 PM
Originally posted by betheball
Any good books you recommend for learning Javascript? I've listed some here (http://www.webxpertz.net/forums/showthread.php3?s=&threadid=26869#post145911). ;)