The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How can I count the total number of results displayed?

I'm currently working on a javascript form validator and have run into a problem. The validator is used to make sure that the customer has entered a number in the quantity input box of the item they wish to purchase.

The part in question is the red highlighted section.
The number of items that is under a specific product is generated dynamically and can range from 2-20. How can I get javascript to count the total number of items and run "for i=0;i<total number" instead so it is different for each product page?

What sort of form control is quantity. Because by the use of that loop, you are treating it as an array. Only checkboxes and radio groups behave this way (SELECTs do too, but through their options collection). By acknowledgement of these facts, your function seems like it would error for sure.

Also, your min/max parameters are useless, since the regexp you specify has those length constraints built in. However, your regexp has something else fishy about it, so I'm not exactly sure what format you want to accept.

For what it's worth, I have written a pretty good form validation script that you may want to check out. I can already see how it can be applied here.

The "Quantity" fields are input fields where they enter in the number they wish to purchase. The input fields are all named "quantity". Since the number of items that are associated with each product can vary, I thought using a loop would be the best method.

The RegExp checks the following:
If a single digit, it must be between 1-9. This prevents people from entering the quantity of 0.
If double or triple digit, then numbers 0-9 are allowed.

Well, yes, javascript can 'know' the number, but only if you take the appropriate steps to put the value into a js variable. I don't know what server-side technology you are using, but with PHP, it would look like this...

or any viable variation. However, if you use fValidate, this data isn't necessary. You will, however, need to modify the HTML output of your form controls to include an additional attribute (or two). Is this something you can do?

beetle, I appreciate all your help.
I did check out fValidate, but it doesn't seem to offer the validation I am looking for.
Also, I am using ASP and yes, if necessary, I could change the output of the html.

Here is a screen shot of the "Qty." fields I am working on.

Basically, I would like to prevent customers from submitting the form if they enter anything other than numeric characters. I would also like to prevent the form from submitting if they leave all fields blank.
Thanks!

Now, how could I add on to this to check and see if at least one "Quantity" field was filled in before submitting? I would assume that I would use arrays, add each fieldToCheck[i].value to an array and then add the values up at the end. Then, if the sum total = 0 or null, then no quantity was entered. How would I do this with javascript?
Thanks again!