<%
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
%>