SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.
    Code:
    function validateRE(fieldToCheck, strRegExp, msg, min, max) {
    	if (!min) { min = 0 }
    	if (!max) { max = 100 }
      
    	for (i=0;i<15;i++)
    	{
    		if (fieldToCheck[i].value) {
    			var re_pattern = strRegExp;
    			if (!re_pattern.test(fieldToCheck[i].value) || fieldToCheck[i].value.length < min || fieldToCheck[i].value.length > max) {
    			  	alert(msg);
    			  	fieldToCheck[i].focus();
    			  	fieldToCheck[i].select();
    			  	return false;
    			}
    		}
    	}
    	return true;
    }
    Used with:
    Code:
    <form method="post" action="../some_URL"
       onsubmit="return (
       
       validateRE(this.quantity, /^[1-9]{1}$|^[0-9]{2,3}$/,
          'Please enter a Quantity.', 1, 3)  
    	  	    
       );">
    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?

    Thanks!
    Keomed.com Webmaster
    www.keomed.com

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    How can I get this to work?
    Keomed.com Webmaster
    www.keomed.com

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Hideki
    How can I get this to work?
    With your script? Or using fValidate?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either way, as long as it worked.
    Can javascript determine the number of results displayed from a database?
    Keomed.com Webmaster
    www.keomed.com

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...
    PHP Code:
    $sql "SELECT * FROM myTable";
    $query mysql_query$sql ) or die( "Error!" );
    $rowCount mysql_num_rows$query );

    echo( 
    "<script>\n" );
    echo( 
    "var dbRows = $rowCount;\n" );
    echo( 
    "</script>\n" ); 
    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 a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

    BTW, the Loop seems to work even though they are input fields.
    Last edited by Hideki; Dec 4, 2002 at 10:23.
    Keomed.com Webmaster
    www.keomed.com

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It certainly does have what you need! Just do steps one and two from here and change your inputs to include these attributes (assuming you don't change the config file, fValConfig.js)
    Code:
    <input ... alt="numberr|1|999" emsg="A custom error message" />
    For more info on the numberr validator, click here
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How can I count the total number of results displayed?

    Beetle, I used your PHP example and got it to work.
    Code:
    function validateRE(fieldToCheck, strRegExp, msg) {
             var aVariable = <%= rsItems.RecordCount %>;
    	for (i=0;i<aVariable;i++)
    	{
    		if (fieldToCheck[i].value) {
    			var re_pattern = strRegExp;
    			if (!re_pattern.test(fieldToCheck[i].value)) {
    			  	alert(msg);
    			  	fieldToCheck[i].focus();
    			  	fieldToCheck[i].select();
    			  	return false;
    			}
    		}
    	}
    	return true;
    }
    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!
    Keomed.com Webmaster
    www.keomed.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •