home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / form_handler.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  7.5 KB  |  223 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. ' I've got some pretty lame validation going here, but I've set
  15. ' it up so you can easily implement any criteria you want.
  16. Function ValidateField(sFieldValue, sFieldType)
  17.     Dim bFieldIsOkay
  18.     
  19.     ' defaut it to true
  20.     bFieldIsOkay = True
  21.     
  22.     ' go to the field name to validate the entry
  23.     Select Case LCase(sFieldType)
  24.         Case "name"
  25.             If Len(sFieldValue) = 0 Then bFieldIsOkay = False
  26.         Case "email"
  27.             If Len(sFieldValue) < 5 Then
  28.                 bFieldIsOkay = False
  29.             Else
  30.                 If Instr(1, sFieldValue, " ") <> 0 Then
  31.                     bFieldIsOkay = False
  32.                 Else
  33.                     If InStr(1, sFieldValue, "@") < 2 Then
  34.                         bFieldIsOkay = False
  35.                     Else
  36.                         If InStrRev(sFieldValue, ".") < InStr(1, sFieldValue, "@") + 2 Then
  37.                             bFieldIsOkay = False
  38.                         End If
  39.                     End If
  40.                 End If
  41.             End If
  42.             ' Previous validation code
  43.             'If Len(sFieldValue) < 5 Then
  44.             '    bFieldIsOkay = False
  45.             'Else
  46.             '    If InStr(1, sFieldValue, "@", 1) < 2 Then
  47.             '        bFieldIsOkay = False
  48.             '    Else
  49.             '        If InStr(1, sFieldValue, ".", 1) < 4 Then
  50.             '            bFieldIsOkay = False
  51.             '        End If
  52.             '    End If
  53.             'End If
  54.         Case "address"
  55.             If Len(sFieldValue) = 0 Then bFieldIsOkay = False
  56.         Case "city"
  57.             If Len(sFieldValue) = 0 Then bFieldIsOkay = False
  58.         Case "state"
  59.             If Len(sFieldValue) <> 2 Then bFieldIsOkay = False
  60.         Case "zip"
  61.             If Len(sFieldValue) <> 5 And Len(sFieldValue) <> 10 Then
  62.                 bFieldIsOkay = False
  63.             End If
  64.             ' Don't use cause of 00000-0000
  65.             'If Not IsNumeric(sFieldValue) Then bFieldIsOkay = False
  66.         Case Else 'if an unknown type gets in reject form!
  67.             bFieldIsOkay = False
  68.     End Select
  69.  
  70.     ValidateField = bFieldIsOkay
  71. End Function
  72.  
  73. ' Little helper function to save me from typing out each field.
  74. ' I'm lazy... so sue me!
  75. Sub ShowFormField(strField)
  76. ' This function needs access to the dictFields object!
  77.     %>
  78.     <TR>
  79.         <TD ALIGN="right"><B><%= strField %>:</B> </TD>
  80.         <TD><INPUT NAME="<%= strField %>" TYPE="text" VALUE="<%= Request.Form(strField) %>"></INPUT></TD>
  81.         <TD><%
  82.         If dictFields(LCase(strField)) Then
  83.             Response.Write "<IMG SRC=""../images/check.gif"" BORDER=""0"" WIDTH=""25"" HEIGHT=""25"">"
  84.         End If
  85.         %></TD>
  86.     </TR>
  87.     <%
  88. End Sub
  89. %>
  90.  
  91. <%' Begin Runtime Code
  92.  
  93. ' If you just want to check the fields and don't care about which fields
  94. ' failed, try a series using this type of syntax:
  95. 'Dim bFormIsOkay 'As Boolean
  96. 'bFormIsOkay = True
  97. 'bFormIsOkay = bFormIsOkay And ValidateField("John", "name")
  98. 'bFormIsOkay = bFormIsOkay And ValidateField("john_doe@some_domain.com", "e-mail")
  99. 'bFormIsOkay = bFormIsOkay And ValidateField("NY", "state")
  100. '
  101. ' At the end, if bFormIsOkay is True, everything checked out, o/w it didn't
  102.  
  103.  
  104. ' I want to maintain a list of failures so my code's not going to be
  105. ' quite as nice and neat, but I'm going to be pulling a different trick
  106. ' to save me some typing!  Check out the For Each Loop!  What do you want?
  107. ' I already said I was lazy!
  108.  
  109. Dim Field      'looping variable
  110. Dim dictFields 'dictionary for failed fields
  111.  
  112. Set dictFields = Server.CreateObject("Scripting.Dictionary")
  113.         
  114. ' We never need to ask for fields by name and their names
  115. ' identify them to the validation script!
  116. For Each Field in Request.Form
  117.     If ValidateField(Request.Form(Field), Field) = False Then
  118.         dictFields.Add LCase(Field), True
  119.     End If
  120. Next 'Field
  121.  
  122. ' Troubleshooting lines - left in for when you break it!
  123. 'Response.Write Request.Form.Count
  124. 'Response.Write dictFields.Count
  125.  
  126. ' Check to be sure fields were entered (Request.Form.Count <> 0)
  127. ' and correctly (dictFields.Count = 0)
  128. ' If so we process the form, o/w we show the form with checks showing
  129. ' fields to be fixed.
  130. If Request.Form.Count <> 0 And dictFields.Count = 0 Then
  131.  
  132. ' Some people have reported problems using Request.Form.Count
  133. ' Validating a required field instead should work just as well:
  134. 'If Request.Form("name") <> "" And dictFields.Count = 0 Then
  135.  
  136.     ' Process Input
  137.     ' This is where you'd actually do something!
  138.     ' I'm not overly creative, so I just write it out to the browser
  139.     %>
  140.     <B>Your entry meets our validation criteria!</B><BR>
  141.     <BR>This would naturally be the point where your just entered data
  142.     would be getting logged to a file, inserted into a database, mailed
  143.     off to someone, or whatever your plans for it might happen to be!
  144.     Since we're just playing with the form here, I simply show it below.<BR>
  145.     <BR>
  146.     <B>Here's what you entered:</B><BR>
  147.     <%
  148.     ' Loop through the fields writing out the field name and value.
  149.     For Each Field In Request.Form
  150.         Response.Write Field & ": " & Request.Form(Field) & "<BR>" & vbCrLf
  151.     Next 'Field
  152. Else
  153.     ' Two possible reasons for us to be here
  154.     ' 1. It's their first visit to the page - show the form with no check marks.
  155.     ' 2. They've entered data and validation failed - show why in form below!
  156.  
  157.     ' If they entered some data, and we're still here, then
  158.     ' they must have made a mistake.  Tell them so:
  159.     If Request.Form.Count <> 0 Then
  160.         %>
  161.         I'm sorry but your form wasn't filled out correctly.
  162.         Please correct the fields indicated by the check marks.
  163.         <%
  164.     End If
  165.     
  166.     ' Show thw form with checks if appropriate
  167.     %>
  168.     <FORM ACTION="<%= Request.ServerVariables("Script_Name") %>" METHOD="post" NAME="TheForm">
  169.  
  170.     <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
  171.     <%
  172.     ' This was done mearly to save myself some typing and to ensure a uniform
  173.     ' look and feel.  You'll find the ShowFormField function directly above
  174.     ' the beginning of the run time code
  175.     ShowFormField("Name")
  176.     ShowFormField("Email")
  177.     ShowFormField("Address")
  178.     ShowFormField("City")
  179.     ShowFormField("State")
  180.     ShowFormField("Zip")
  181.     %>
  182.     </TABLE>
  183.     <INPUT TYPE="reset" VALUE="Reset The Form"></INPUT>
  184.     <INPUT TYPE="submit" VALUE="Submit The Form"></INPUT><BR>
  185.     <BR>
  186.     <INPUT TYPE="button" VALUE="Enter Invalid Data!" OnClick="return FillInTheFormBad()"></INPUT>
  187.     <INPUT TYPE="button" VALUE="Enter Valid Data!" OnClick="return FillInTheFormGood()"></INPUT><BR>
  188.     </FORM>
  189.     <%
  190. End If
  191.  
  192. ' Just to make life a little easier for our visitors, we've added two buttons.
  193. ' One's attached to each script below.  The first simulates a user typing in
  194. ' information which fails to meet or criteria in a couple of places.  The second
  195. ' simulates data which, as far as were concerned, is fine for whatever
  196. ' processing we decide to do to it.
  197. %>
  198. <SCRIPT LANGUAGE="Javascript">
  199. <!--
  200. //Us doing client-side script!  Did you ever thing you'd see the day?
  201.  
  202. //Enter Bad Data Into the Form
  203. function FillInTheFormBad() {
  204.     document.TheForm.Name.value = "John";
  205.     document.TheForm.Email.value = "john@some_domaincom";
  206.     document.TheForm.Address.value = "123 Main Street";
  207.     document.TheForm.City.value = "Any Town";
  208.     document.TheForm.State.value = "New York";
  209.     document.TheForm.Zip.value = "1111";
  210. }
  211.  
  212. //Enter Good Data Into the Form
  213. function FillInTheFormGood() {
  214.     document.TheForm.Name.value = "John";
  215.     document.TheForm.Email.value = "john@some_domain.com";
  216.     document.TheForm.Address.value = "123 Main Street";
  217.     document.TheForm.City.value = "Any Town";
  218.     document.TheForm.State.value = "NY";
  219.     document.TheForm.Zip.value = "11111";
  220. }
  221. //-->
  222. </SCRIPT>
  223.