adOpenStatic Logo
Navigation
Home
FAQ
Ken's Blog
Resources
Contact Ken
Copyright 2000 -



Generating the error message

<%
If isPostBack() then

   ' Perform server-side validation here

   ' If no errors after validation then

      ' Do database/etc work
      ' Redirect to next page

   ' Else load form values

Else

   ' Load default/initial form values

End If
%>

<!-- Load default page header/body/navigation etc -->
<!-- Load error messages -->
<!-- Load javascript validators -->
<!-- Load HTML form -->
<!-- Load default page footer etc -->

 

 

 

 

 
 

 

 

 

 

 
 
 
Looking at this part.


We now look at the final part - generating a suitable error message to be sent to the client is there is an error. At this stage we have both an Dictionary object containing error messages, and a comma separated list of errors codes (representing errors) that have occured. We use a simple function to split this comma separated list into a VBScript array, and then iterate this array, finding the corresponding matching error code and message in the master errors Dictionary object. We then write it to the screen in an appropriate format.

In our HTML page, we change what we had to include a couple of extra lines. Before we had:

<!-- Your page header/navigation etc HTML goes here - eg loaded via SSI -->
<body>
<%
   Call WriteFormHeader("Form1Validator")

and so forth. Now, we include a couple of extra lines, like so:

<!-- Your page header/navigation etc HTML goes here - eg loaded via SSI -->
<body>
<%
   If not isEmpty(errCode) then
      Call WriteUserErrors(objErrors, errCode, _
         "An error has occurred. Please review the following list and resubmit the form.")
   End If

   Call WriteFormHeader("Form1Validator")

This extra sub will only be called in the event that the page has been reloaded. However, if everything has gone OK (ie there were no errors in server-side validation) then we would have been redirected to the next page - and this sub wouldn't be called. Thus the only time that this sub would be called is if the page has been reloaded, and server side validation picked up errors.

The actual code used by WriteUserErrors is fairly simple:

<%
'----------------------------------------
' Takes the values of the error codes in errCode, finds the
' matching error codes in objErrors and writes out the corresponding error message
' Accepts objErrors as Scripting.Dictionary - master list of errors
' Accepts errCode as comma delimited string - error codes separated by commas
' Accepts errMessage as string - master error message to be displayed
' © Ken Schaefer (adOpenStatic.com) 2001
'----------------------------------------


Sub WriteUserErrors( _
   ByRef objErrors, _
   ByVal errCode, _
   ByVal errMessage _
   )

   On Error Resume Next
   Const Proc = "WriteUserErrors"
   Dim arrErrors
   Dim i
   arrErrors = Split(errCode, ",")

   Response.Write( _
      "<p><span class=""Bold"">" & errMessage & "</span></p>" & vbCrLf & _
      "<ul>" & vbCrLf)

   For i = 0 to UBound(arrErrors)
      Response.Write("<li>" & objErrors(arrErrors(i)) & "</li>" & vbCrLf)
   Next

   Response.Write("</ul>")

   If Err.Number <> 0 then
      Call subWriteError(Err.Number, Proc, Err.Description)
   End if

End Sub ' WriteUserErrors
%>

Back to Server Side validation | Putting it all together

Back to Code Listing