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!

Validation - Form re-display

Status
Not open for further replies.

croydon

Programmer
Apr 30, 2002
253
EU
I am writing a small asp program to read the contents of a form, validate it and send an email.

The problem I have is with the validation. Obviously I only want any error message to be displayed after the Submit button is pressed. At present the validation checking occurs before the form is displayed and therefore shows an error message before the user enters any data.

Is there a 'standard' way of doing this? Thanks.
 
You could use Javascript to validate the form, or you could see if the page was requested by a GET (browse to it) or POST (Form submission) or you could add a status flag as a hidden field.
 
OR when you have no knowledge of Javascript, you could put your current page (where the form is situated) in the form-action-field with the result of having the page getting the information filled in the form. Then you can simply validate the information in the background with some basis VBscript. It's easy and very customisable. When all validation is done you send the variables to a database/mail. Otherwise you could simply warn the users of errors and point to them using vbscript on the page.

Hope you understand it a bit...(sorry about my bad english)
 
Thanks for the help.
I already had the form and email working but just the validation was missing. I have now included some javascript code (that I found on another thread) so that message boxes appear when errors occur.
Problem solved.

 
The only problem with depending directly on javascript for your validation is that someone could still spoof a form post to your processing page by simply copying the source of your form page and removing the javascript and then they could post anything they want, breaking your processing page or even sending illegitimate information (for whatever purpose).

A solution to your earlier problem would be to add a hidden field to your form page and have the form post back to itself.
When the page loads check the value of the hidden variable, if it has a value then do your validation routines and keep a boolean variable handy to switch if there are errors present. At the end of the page use a Response.Clear and Response.Redirect "proccesingpagename.asp" if there were no errors.

You have two options for the page layout, either include the validation directly into the form line by line and check the hidden field each time (or a variable that denotes whether your validating or not) or create a copy of the form for entering data and a copy for validation and re-entry.

Here is an example of the line by line method:
Code:
<%
Option Explicit

Dim validation_on, validation_result

'check for the hidden field, another option would be to check value of submit button
If Request.Form("hdnValidationFlag") <> "" Then 
	validation_on = True
Else
	validation_on = False
End If

validation_result = True

'I'm placing everything in Response.Writes just so i wont have to switch back and forth between ASP and HTML as much
Response.Write "<html><body>"

'Start the form
Response.Write "<form method=""POST"" action=""MyForm.asp"">"
'Output the hidden field
Response.Write "<input type=""hidden"" name=""hdnValidationFlag"" value=""Whatever"">"

'Output the form with item-by-item validation

	'--- Name Field
		'do some validation on this field
		If validation_on Then
			If len(Request.Form("txtName")) = 0 Then
				Response.Write "<span style=""color:red;"">*</span>"
				validation_result = false
			End If
		End If

		'output the field - this will work okay for first run and validation runs but keeps the HTML in one place for easier future changes
		Response.Write "Name: <input type=""text"" name=""txtName"" value=""" & Request.Form("txtName") & """><br>"

	'--- Email Address Field
		'do some validation - normally I would use a Regular Expression, but this is just an example
		If validation_on Then
			If len(Request.Form("txtEmail")) = 0 Then
				Response.Write "<span style=""color:red;"">*</span>"
				validation_result = false
			End If
		End If

		'output the field
		Response.Write "Email: <input type=""text"" name=""txtEmail"" value=""" & Request.Form("txtEmail") & """><br>"


	'--- Submit button
	Response.Write "<input type=""submit"" value=""Submit My Data"">"

Response.Write "</form></body></html>"

'If the validation is on and the result is true, we don't need the above output so clear the Response buffer and transfer to the processing page
If validation_result And validation_on Then
	Response.Clear
	Server.Transfer "processing.asp"		'Server.Transfer happens server-side so Request.Form values will still be available
End If
%>


And then here is an example that has two copies of the form:
Code:
<%
Option Explicit

Dim validation_on, validation_result

If Request.Form("hdnValidationFlag") <> "" Then validation_on = True
validation_result = True

'I'm placing everything in Response.Writes just so i wont have to switch back and forth between ASP and HTML as much
Response.Write "<html><body>"

'Start the form
Response.Write "<form method=""POST"" action=""MyForm.asp"">"
'Output the hidden field
Response.Write "<input type=""hidden"" name=""hdnValidationFlag"" value=""Whatever"">"

'Output the first entry form
If Not validation_on Then
	Response.Write "Name: <input type=""text"" name=""txtName""><br>"
	Response.Write "Email: <input type=""text"" name=""txtEmail""><br>"
Else	'output the validation form/do validation

	'--- Name Field
		'do some validation on this field
		If len(Request.Form("txtName")) = 0 Then
			Response.Write "<span style=""color:red;"">*</span>"
			validation_result = false
		End If
		Response.Write "Name: <input type=""text"" name=""txtName"" value=""" & Request.Form("txtName") & """><br>"


	'--- Email Address Field
		'do some validation - normally I would use a Regular Expression, but this is just an example
		If len(Request.Form("txtEmail")) = 0 Then
			Response.Write "<span style=""color:red;"">*</span>"
			validation_result = false
		End If
		Response.Write "Name: <input type=""text"" name=""txtName"" value=""" & Request.Form("txtName") & """><br>"

		If validation_result and validation_on Then
			Response.Clear
			Server.Transfer "processing.asp"
		End If
End If

	'--- Submit button
	Response.Write "<input type=""submit"" value=""Submit My Data"">"

Response.Write "</form></body></html>"
%>

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top