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!

I need to Validate a form on a calculation.

Status
Not open for further replies.

Vermontecher

Programmer
Feb 13, 2005
14
0
0
US
Hello,
This is an awesome site, I am hoping one of you wizards have the answer to my quandry. I have a form that has two fields I need to calculate and validate. What I want is something like this...

Background: Access 2000 Database
Form developed in ASP
Total of many fields but only two are at issue...

Request.form("NumberPurchased")
If NumberPurchased >= ("Available") Then
'The error will go here
Else
Process the form


So basically I want to make sure that the purchaser can not go any farther with the form if they enter a number larger than the number of available items. If there are 6 items available and the visitor enters a 7 or higher it will pop up an error, if they enter a 6 or less it will process the form. I know how to do the calculation (I think) but I do not have a clue how to validate it when the submit button is clicked.

Thanks, I look forward to many visits to this site in the future.

VermontTecher
 
Hello,
I guess I didn't make myself clear... I have hit a wall, I am looking for help on the Submit part. I don't know how to make this work when the submit button is pressed. I have spent literally hours searching and reading trying to find the script that would run but no luck. If you can just point me to an example of one that works then I can make it happen, but I would really like some direction on this. I need to have this script process when a visitor presses Submit and not go on if the above value is not met.

Thanks
 
Hello,
This form is very long, there are a lot of fields in it. I have taken the three fields out that are rlevant to what I am doing and hope this is enough. If not then please let me know and I will post the entire code here... the problem is it is over 1400 lines of code without wordwrap set so on here it would be very long. Let me know if I need to provide anything else please...

<form name="Ticketsedit" id="Ticketsedit" action="purchase.asp" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_edit" value="U">
<input type="hidden" name="key" value="<%= sKey %>">
<table border="0" cellspacing="1" cellpadding="4" width="500" bgcolor="#CCCCCC">
<% Dim ThisSale,PreviousSale,TotalSales %>
<%
If ThisSale = "" Then ThisSale = Request.form("x_NumberPurchased")
PreviousSale = x_NumberPurchased
TotalSales = (x_TotalTicketsPurchased) + (PreviousSale)
%>
<% Available=(x_NumberOfTickets) - (TotalSales) %>
<tr>
<td bgcolor="#000000"><font face="Verdana" size="-2" color="#FFFFFF">Number Of Tickets Available</span></td>
<td bgcolor="#F5F5F5"><font face="Verdana" size="-2" color="#000000">
<% Response.Write Available %><input type="hidden" id="x_NumberOfTickets" name="x_NumberOfTickets" value="<%= x_NumberOfTickets %>">
</span></td>
</tr>
<tr>
<td bgcolor="#000000"><font face="Verdana" size="-2" color="#FFFFFF">Number Requested</span></td>
<td bgcolor="#F5F5F5"><font face="Verdana" size="-2" color="#000000">
<input type="Text" name="x_NumberPurchased" id="x_NumberPurchased" size="30" maxlength="255" value="">
</span></td>
</tr>
<tr>
<td bgcolor="#000000"><font face="Verdana" size="-2" color="#FFFFFF">Total Tickets Purchased</span></td>
<td bgcolor="#F5F5F5"><font face="Verdana" size="-2" color="#000000">
<input type="Hidden" name="x_TotalTicketsPurchased" id="x_TotalTicketsPurchased" size="30" value="<%= TotalSales %>"><%= TotalSales %>
</span></td>
</tr>
</table>
<p>
<input class="bginput1" type="submit" name="Action" value="Complete This Purchase">
</form>
 
You'll have to amend the purchase.asp page. You could put in some client-side code to check it, but someone could use their own form to send the info.
 
Hello,
What do you mean exactly? I guess I don't understand.
 
The page with the form on submits data to purchase.asp. You'll need to put your check in there. What does your purchase.asp page say?
 
If you're looking for help doing client-side scripting, you're in the wrong forum. Javascript is the universal client-side scripting language, but if you're sure your target audience is 100% IE users, you can use VBScript. You should ask this questions, with the actual HTML output (not ASP tags), in the appropriate forum.

Lee
 
Ok, here is the deal... I may be asking the wrong questions but I really don't think so.

1.) The purchase.asp page is the form. What I want it to do is check the request.form("NumberPurchased") when Submit is pressed against Available and if NumberPurchased is larger than Available then it will give an error. If the NumberPurchased is equal to or less than Available then the form will process.

2.) I would like to do this in ASP and not Javascript Client Side Scripting. If that is not possible then I will move on the the forum for Javascript, but if it is possible then I want to do it in ASP in the form when it is Submitted.

I think I could (Notice "think") create a validate.asp file that I could have this form run through but that isn't what I want to do either. I will if that is the best way to do this in ASP.

Thanks for your help, I do appreciate your time in replying to this post.
 
If you want the check to happen before the page is actually submitted, and you know the number of "Available" at the time that the form is sent to the user, then you can use client-side authentication.

If, on the other hand, the number of "Available" is subject to change, then your code needs to be on the server.

The thing that we need to be very clear on is that once the ASP page is sent to the user, you cannot check the values of server-side variables... it becomes a static page loaded in the user's browser.

So, if the value of "Available" is static, you can use the Active Server Page to dynamically write the client-side script... If the value of "Available" fluxuates over time and must be determined by a database lookup or somesuch technique, then it can only be processed on the server. In this latter case it means that your form will have to actually be submitted and then the page that it is subbmitted TO will decide whether or not the user's value meets the criteria as it stands at that actual time.
 
Hello Sheco,
Thanks so much for the reply. I do want this to happen before the posting is submitted, when the visitor submits the form. I think I have a better understanding now, and by the looks of things I may not be able to do what I want to on the page I want to do it on. Here are the parameters I am working in...

First let me say this, I am building a site to sell tickets. I have the site completed to this point, and would like to get by this one little quirk. A user ads their data and submits it. The user inputs into the table a value for the amount of tickets they have for sale. When a purchaser tries to make a purchase I want them to only be able to purchase a quantity up to the amount available. The problem is if someone has 20 tickets and there are 5 different purchasers I am trying to keep a running tally of what is available. I have that calculation working properly and have assigned the value the variable Available.

1.) Available is derived from the table through a calculation, but I believe it is static on the page. At least for the time being it is. I am not checking the value in the database when the form is submitted, it is checked when the form is loaded and posted to the page in the variable Available. I can see where this may be an issue if someone tries to but the tickets while another person is also making the same purchase at the same time, but that is another issue for another day.

2.) The calculation for Available is taking the amount originally submitted by the seller, reducing it by the amounts sold to date and returning a value assigned to Available.

All of this is being processed in the same page. There is only one form.

If you think this is a whole lot easier in the Javascript forum then let me know and I will move on to that one.

Thanks again.

 
So the form and the processing happens on the same page? In which case, just put the check at the top.

Post the entire ASP page if you not sure (although you may have to post a link to the source)

Jon
 
If "Available" is always the same then you can just validate in client-side script like this:
Code:
<html>
	<head>
		<title>Test Page</title>
		<script language="javascript">
			function SubmitOK()
			{
				if (document.myForm.myField.value  == "secret")
				{
					return(true);
				}
				else
				{
					alert("Thats not it!");
					return(false);
				}
			}
		</script>
	</head>
	<body>
		<form name="myForm" onSubmit="return SubmitOK();" action="[URL unfurl="true"]http://www.somethingawful.com">[/URL]
			Type the secret word here:
			<input type="test" name="myField">
			<br>
			<input type="submit" value="Client-Side Validation">
		</form>
	</body>
</html>

If Available changes, but only when you change the code of the website itself then you can use ASP to help write the client side code like this:
Code:
<%
  Dim mySecretValue

  mySecretValue = "foo"
%>
<html>
	<head>
		<title>Test Page</title>
		<script language="javascript">
			function SubmitOK()
			{
				if (document.myForm.myField.value  == "[b]<%= mySecretValue %>[/b]")
				{
					return(true);
				}
				else
				{
					alert("Thats not it!");
					return(false);
				}
			}
		</script>
	</head>
	<body>
		<form name="myForm" onSubmit="return SubmitOK();" action="[URL unfurl="true"]http://www.somethingawful.com">[/URL]
			Type the secret word here:
			<input type="test" name="myField">
			<br>
			<input type="submit" value="Client-Side Validation">
		</form>
	</body>
</html>

If the value of Available is variable, it must be check at the time the form is actually submitted, because it may have changed between the time the page was created by your web server and the time the user actually submits the form. The user might have left the page open and gone to a meeting or taken a lunch-break...

Anyway, in this case you need to actually submit the form back to the server... lets call it MyFormPage.asp :
Code:
The form page might look like this:
<html>
	<head>
		<title>Test Page</title>
	</head>
	<body>
		<form name="myForm" onSubmit="return SubmitOK();" method="post" action="MyProcessingPage.asp">
<%
  IF (Request.QueryString("reject") = "yes") THEN
    Response.Write "<red>Thats not it!</red><br><br>"
  END IF
%>

			Type the secret word here:
			<input type="test" name="myField">
			<br>
			<input type="submit" value="Client-Side Validation">
		</form>
	</body>
</html>



And the page to process your form might look like this:
<%@ Language=VBScript %>
<%
  Dim mySecretValue, adoRS, adoCN

  '*** Here you just have to imagine the code for querying
  '*** a database to get the value for "Available"

  mySecretValue = adoRS("AvailableDataBaseField")

  IF (Request.Form("myField") <> mySecretValue) THEN
    'return the user back to the form page
    Response.Redirect "MyFormPage.asp?reject=yes"
  ELSE
    '*** Here you imagine doing whatever else you want to 
    '*** do witht the submitted form data.
  END IF
%>

No this example uses two pages, another way to do it is to make a page submit to itself... we won't get into that unless that is what you are trying to do.
 
Correction, the button on the third example should have had the value "Server-Side Validation
 
I think that perhaps you do indeed want a page that submits to itself... and if you have that then you might as well check the value at the submit time... because of the multi-user ticket sales issue. You certainly don't want to sell more tickets than you have.

Code:
<%@ Language=VBScript %>
<%
	'*** This page will be processed at least twice for each user.
	'*** Once when the page is initially sent to the user, and
	'*** again for each time it is submitted.  It may be submitted
	'*** more than once if the user has to try several times get a 
	'*** valid amount.
	
	'* establish connection to the database ****************
  Dim oCN
  Set oCN = Server.CreateObject("ADODB.Connection")
	oCN.Open "<Put your ADO ConnectString here>"
  
  IF (oCN.State <> 1) THEN
		set oCN = nothing
		Response.Write "ADO Connection Error."
		Response.End 
  END IF
	'*******************************************************



	'* fetch available ticket count from database **********
	Dim oRS
	Set oRS = Server.CreateObject("ADODB.Recordset")
	Set oRS.ActiveConnection = oCN

	oRS.Open "SELECT Count(*) FROM TicketTable WHERE PurchaseDate is Null"

	'check for error:
  IF (oRS.State <> 1) THEN
		oCN.Close 
		set oRS = nothing
		set oCN = nothing  
		Response.Write "ADO Recordset Error."		
		Response.End 
  END IF

	'another error check:
 	IF oRS.EOF THEN
		oRS.Close 
		oCN.Close 
		set oRS = nothing
		set oCN = nothing 	
		Response.Write "ADO Recordset Error."			
		Response.End 
	END IF
	
	'Avaiable ticket count is the 1st field in oRS:
	Dim AvailableTicketCount
	AvailableTicketCount = oRS(0)   
	oRS.Close 
	oCN.Close 
	set oRS = nothing
	set oCN = nothing 	
	'*******************************************************	
	

	
	'* Process the submitted form.  If the number of tickets
	'* requested is not valid, prepare an error message for 
	'* the user.  If it IS valid then update the database and
	'* redirect the browser to a thank-you page.
	Dim MessageToUser
	
	IF (Len(Request.Form("SubmittedToSelf")) > 0) THEN
		'The page was submitted:
		IF Not IsNumeric(Request.Form("myField")) THEN
			MessageToUser = "Please enter a valid number.<BR><BR>"
		ELSE
			IF (AvailableTicketCount < Request.Form("myField")) THEN
				MessageToUser = "Sorry, not enough remaining tickets.  Try again.<BR><BR>"
			ELSE
				'*** The # of tickets is valid.
				
				'*** Add the code here to update your database of sold tickets.
				
				'now send the user to a "thank you" page:
				Response.Redirect "ThanksForBuyingTickets.asp"
				Response.End 
			END IF
		END IF
	END IF
	
	MessageToUser = MessageToUser & "There are " & AvailableTicketCount & " tickets remaining."
%>
<html>
	<head>
		<title>Test Page</title>
	</head>
	<body>
		<center><%= MessageToUser%></center>
		<br>
		<br>
		<form name="myForm" onSubmit="return SubmitOK();" method="post" action="ThisPageSubmitsToItself.asp">
			How many tickets do you want?:
			<input type="test" name="myField">
			

			<br>
			<input type="submit" name="SubmittedToSelf" value="Server-Side Validation">
		</form>
	</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top