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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form calculate and equality check

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
So, I have a form where people can enter the number of t-shirts they for want each size and I have a script that sums it up as they tab off of each form element. I also send in the number of total registrants they have to that page. What I would like to do is check on form submission to make sure that the total number of shirts they have requested is equal to the number of registrants that they have.

So, here is the incoming number of spaces they have
Code:
Dim num :  num = request("num")

Here is the script that does the calculation
Code:
	<script language="JavaScript" type="text/javascript">
	function calculate(el)
	{
		var form = el.form;
		var xsmall = Number(form.elements["xsmall"].value );
		var small = Number(form.elements["small"].value );
		var medium = Number(form.elements["medium"].value );
		var large = Number(form.elements["large"].value );
		var xlarge = Number(form.elements["xlarge"].value );
		var xxlarge = Number(form.elements["xxlarge"].value );
		var total = xsmall + small + medium + large + xlarge +xxlarge;
		  
		// Check for user entry errors
		if (isNaN(total)) {
			// Alert user of error
			alert("Please enter only numbers");
		} else {
			// Show total in the form field, and select the form field text
			form.elements["total"].value = total;
			//form.elements["total"].select();
		}
	  
	}		
    </script>

Here is the call to the script in the code
Code:
<input name="total" type="text" id="total" size="4" onfocus="calculate(this)">

and here is the call to submit the page
Code:
<input type="image" name="submit" id="submit" src="/images/submit_blue.gif" onmouseover='this.src="/images/submit_green.gif"'; onmouseout='this.src="/images/submit_blue.gif"'; value="Submit &gt;&gt;" width="112" height="32">

Any ideas, thoughts or suggestions to make this work would be very much appreciated.

Thanks,
Willie
 
After your submit, the 2nd page could retrieve values from the previous one with eg request.form("total")

Your first page has this Javascript caluculation/ check function, but you should program defense, so do additional checking in the 2nd page.

a=request.form("xsmall")
b=request.form("small")
etc

check if the are all valid numbers, etc.
 
I would like to handle it all on the first page using something like onSubmit() so they can't actually get off of the page if they have entered too large of a number.

wb
 
the classic trick for that is to start the program with the [Submit] button disabled. In your calculate() function you determine if all fields are enterd oke, and if yes you enable the submit button.
 
So, what I am trying to figure out, then, is how to modify the calculate function to determine if it matches the value sent in the query string. I did not write the function and am not sure how to modify it to achieve my goal. The problem that I see is that it does a running total, so I think I can really only check inside of that function to see if the total is greater than the number passed into the form. Which I don't really know how to do in javascript.

wb
 
you can try this method which i'm using:
on page1, when they click submit, have a java function that validates the values. if valid, then proceeds to page2, if not valid, then alerts an error message and stays on page1.
here's the code:
Code:
function SubmitOK()
{            
var bOK = true; 
var somefield = value_of_the_field;     
if (somefield == nogood)
   {
    alert ("somefiled has bad value.");
           bOK = false;
   }
if (bOK == true)
   { 
    return true; 
   }
else
   {
    return false;
   } 
}

................

<form name="frm1" action="page2.asp" method="post" onSubmit="return SubmitOK();">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top