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

Validation sample - server side?

Status
Not open for further replies.

LaPluma

Programmer
Feb 3, 2002
139
DE
Hello

I have a server side script which checks that form fields have been completed. If not, an error page is called pointing out that 'Name' (for example) must be completed, and invites the user to 'go back' to complete the fields correctly.

However, I think we have all seen those error messages (normally in red type) next to the fields themselves - in other words, not another, separate asp file inviting the user to 'go back'.

In fact, some of the better sites keep those fields, which HAVE been completed properly, intact - the visitor doesn't need to complete them again because only the erroneous fields are highlighted.

Has anybody on the forum any sample script (it needs to be server side, since I already have a couple of Javascripts on the page) which would serve my needs?

Thank you in anticipation.

LaPluma
 
Basically in a case like this you can have the form and the "next page" in the same file along with your validation script, something like this:
Code:
<%
Option Explicit

Dim validateFlag 'controls wether to check the form values as we set them later on
validateFlag = False

'Check if our sonn-to-be-set hidden value &quot;next_action&quot; is set. If it is than this is not the first visit and we attempt to validate, if it isn't then we just show them the form
If Request.Form(&quot;next_action&quot;) <> &quot;&quot; Then
	If ValidateForm() = True Then
		'do your successful validation code here
	Else
		'setting validateFlag to true means we will check each field later as we output the fields
		validateFlag = True
		ShowForm
Else
	ShowForm
End If

'--- ShowForm Function ---	
Function ShowForm
	%>
	<html>
	<body>

	<!-- Our HTML form -->
	<form method=&quot;POST&quot; action=&quot;mypage.asp&quot;>
		<!-- This is so we know this will not be the first visit after it is submitted -->
		<input type=&quot;hidden&quot; name=&quot;next_action&quot; value=&quot;validate&quot;>
		<%
		'check if we are supposed to be outputting red stars
		If validateFlag = True Then
			'check each input
			If Request.Form(&quot;txtUsername&quot;) == &quot;&quot; Then
				Response.Write &quot;Username: <input type='text' name='txtUsername'><span style='color:red'>*</span><br>&quot;
			Else
				Response.Write &quot;Username: <input type='text' name='txtUsername' value='&quot; & Request.Form(&quot;txtUsername&quot;) & &quot;'><br>&quot;
			End if

			'check each input
			If Request.Form(&quot;txtPassword&quot;) == &quot;&quot; Then
				Response.Write &quot;Password: <input type='text' name='txtPassword'><span style='color:red'>*</span>&quot;
			Else
				Response.Write &quot;Password: <input type='text' name='txtPassword' value='&quot; & Request.Form(&quot;txtPassword&quot;) & &quot;'>&quot;
			End if

		Else
			%>
			Username: <input type=&quot;text&quot; name=&quot;txtUsername&quot;><br>
			Password: <input type=&quot;text&quot; name=&quot;txtPassword&quot;><br>
			<%
		End If
		%>
		<input type=&quot;submit&quot; value=&quot;Log In&quot;>
	</form>
    <!-- End of HTML Form -->
	</body>
	</html>
	<%
End Function

'---- A validate function ---
'Here is where you would put your full form validation function to check all the fields and return either a true or false for the whole form.

This is just one way to do it. The basic principle is that you want to be able to recall the form without using Response.Redirect and you will need to evaluate the form fields on a per-field basis to see which ones cause the validation to fail.

I hope this helps (I'm a bit tired so it may be confusing),
-Tarwn ________________________________________________
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top