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



Bringing it all together
So, after all that, what does our page look like? These are the details about our form:

  • It has three fields
  • The first two are required, the third is not
  • The 2nd field has a minimum of 5 characters
  • Both the first two fields have a maximum of 50 characters, the 3rd has a maximum of 10 characters
  • The third field can only contain numeric characters

So our code will look like this (some whitespace, eg indenting, may have been removed to counter excessive line length. This is too avoid excessive wrapping in 800x600 displays).

<%@ Language=VBScript %>
<%
Option Explicit
%>
<!-- #include virtual="/inc/HTML/Writepage.asp" -->
<!-- #include virtual="/inc/HTML/WriteFormValidator.asp" -->
<!-- #include virtual="/inc/ASPFormat/UIValidate.asp" -->
<!-- #include virtual="/inc/Errors/UserErrors.asp" -->
<%
Dim strTxt1 ' as string - holds value of form field 1
Dim strTxt2 ' as string - holds value of form field 2
Dim strTxt3 ' as string - holds value of form field 3
Dim objErrors ' as Scripting.Dictionary - holds master list of errors
Dim errCode ' as string - holds list of errors encountered

If isPostBack() then

   '----------------------------------------
   ' --- Save our form input to local variables
   '----------------------------------------
   strTxt1 = Request.Form("txt1")
   strTxt2 = Request.Form("txt2")
   strTxt3 = Request.Form("txt3")

   '----------------------------------------
   ' --- Create our master list of errors
   '----------------------------------------
   Set objErrors = Server.CreateObject("Scripting.Dictionary")
   objErrors.Add "strTxt1-Missing", "You did not enter a value in the 1st field"
   objErrors.Add "strTxt2-TooShort", "Please enter at least 5 characters in the 2nd field"
   objErrors.Add "strTxt3-TooLong", "Please enter no more than 10 characters in the 3rd field"
   objErrors.Add "strTxt3-Invalid", "Please enter only digits (0-9) in the 3rd field"

   '----------------------------------------
   ' --- Begin UI Validation
   '----------------------------------------
   If not TestMinLength(strTxt1, 1) then errCode = AddUserError(errCode, "strTxt1-Missing")
   If not TestMaxLength(strTxt1, 50) then strTxt1 = Left(strTxt1, 50)

   If not TestMinLength(strTxt2, 5) then errCode = AddUserError(errCode, "strTxt2-TooShort")
   If not TestMaxLength(strTxt2, 50) then strTxt2 = Left(strTxt2, 50)

   If not TestMaxLength(strTxt3, 10) then errCode = AddUserError(errCode, "strTxt3-TooLong")
   If not IsAcceptableValues(strTxt3, "0123456789", 0, 0) then _
      errCode = AddUserError(errCode, "strTxt3-Invalid")

   If isEmpty(errCode) then

      ' No errors, so proceed
      ' Insert data into DB, and redirect to next page


   Else

      ' Some errors so reload form

   End If
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <!-- page specific stuff, eg meta description/title here -->
      <title>Validating User Intput - adOpenStatic.com</title>
      <% Call WritePageHeader() %>
   </head>

   <body>
   <%
   Call WritePageBody()

   If not isEmpty(errCode) then
      Call WriteUserErrors(errCode, arrErrors, "An error occurred")
   End If

   '----------------------------------------
   ' --- Begin write client-side validator
   '----------------------------------------
   Call WriteFormHeader("Form1Validator")
   Call WriteFormFieldReqd("txt1", "1st field")
   Call WriteFormFieldReqd("txt2", "2nd field")
   Call WriteFormFieldMinLength("txt2", 5, "2nd field")
   Call WriteFormFieldAccept("txt3", "0123456789", _
      "Please enter only 0-9 values in the 3rd field")
   Call WriteFormFooter()
   '----------------------------------------
   ' --- End write client-side validator
   '----------------------------------------
   %>

   <form method="post" action="page1.asp" name="Form1" onSubmit="return Form1Validator(this)">
   <input type="hidden" name="postback" value="<% =GetFileName()%>"><br>
      1st field: <input type="text" name="txt1" value="<% =strTxt1%>"><br>
      2nd field: <input type="text" name="txt2" value="<% =strTxt2%>"><br>
      3rd field: <input type="text" name="txt3" value="<% =strTxt3%>"><br>
      <input type="Submit">
   </form>

   <% Call WritePageFooter() %>

   </body>
</html>

Generating the Error | Online Test

Back to Code Listing