%
'*******************************************************
'* ASP 101 Sample Code - http://www.asp101.com/ *
'* *
'* This code is made available as a service to our *
'* visitors and is provided strictly for the *
'* purpose of illustration. *
'* *
'* http://www.asp101.com/samples/license.asp *
'* *
'* Please direct all inquiries to webmaster@asp101.com *
'*******************************************************
%>
<%
' I've got some pretty lame validation going here, but I've set
' it up so you can easily implement any criteria you want.
Function ValidateField(sFieldValue, sFieldType)
Dim bFieldIsOkay
' defaut it to true
bFieldIsOkay = True
' go to the field name to validate the entry
Select Case LCase(sFieldType)
Case "name"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "email"
If Len(sFieldValue) < 5 Then
bFieldIsOkay = False
Else
If Instr(1, sFieldValue, " ") <> 0 Then
bFieldIsOkay = False
Else
If InStr(1, sFieldValue, "@") < 2 Then
bFieldIsOkay = False
Else
If InStrRev(sFieldValue, ".") < InStr(1, sFieldValue, "@") + 2 Then
bFieldIsOkay = False
End If
End If
End If
End If
' Previous validation code
'If Len(sFieldValue) < 5 Then
' bFieldIsOkay = False
'Else
' If InStr(1, sFieldValue, "@", 1) < 2 Then
' bFieldIsOkay = False
' Else
' If InStr(1, sFieldValue, ".", 1) < 4 Then
' bFieldIsOkay = False
' End If
' End If
'End If
Case "address"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "city"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "state"
If Len(sFieldValue) <> 2 Then bFieldIsOkay = False
Case "zip"
If Len(sFieldValue) <> 5 And Len(sFieldValue) <> 10 Then
bFieldIsOkay = False
End If
' Don't use cause of 00000-0000
'If Not IsNumeric(sFieldValue) Then bFieldIsOkay = False
Case Else 'if an unknown type gets in reject form!
bFieldIsOkay = False
End Select
ValidateField = bFieldIsOkay
End Function
' Little helper function to save me from typing out each field.
' I'm lazy... so sue me!
Sub ShowFormField(strField)
' This function needs access to the dictFields object!
%>
<%= strField %>:
<%
If dictFields(LCase(strField)) Then
Response.Write ""
End If
%>
<%
End Sub
%>
<%' Begin Runtime Code
' If you just want to check the fields and don't care about which fields
' failed, try a series using this type of syntax:
'Dim bFormIsOkay 'As Boolean
'bFormIsOkay = True
'bFormIsOkay = bFormIsOkay And ValidateField("John", "name")
'bFormIsOkay = bFormIsOkay And ValidateField("john_doe@some_domain.com", "e-mail")
'bFormIsOkay = bFormIsOkay And ValidateField("NY", "state")
'
' At the end, if bFormIsOkay is True, everything checked out, o/w it didn't
' I want to maintain a list of failures so my code's not going to be
' quite as nice and neat, but I'm going to be pulling a different trick
' to save me some typing! Check out the For Each Loop! What do you want?
' I already said I was lazy!
Dim Field 'looping variable
Dim dictFields 'dictionary for failed fields
Set dictFields = Server.CreateObject("Scripting.Dictionary")
' We never need to ask for fields by name and their names
' identify them to the validation script!
For Each Field in Request.Form
If ValidateField(Request.Form(Field), Field) = False Then
dictFields.Add LCase(Field), True
End If
Next 'Field
' Troubleshooting lines - left in for when you break it!
'Response.Write Request.Form.Count
'Response.Write dictFields.Count
' Check to be sure fields were entered (Request.Form.Count <> 0)
' and correctly (dictFields.Count = 0)
' If so we process the form, o/w we show the form with checks showing
' fields to be fixed.
If Request.Form.Count <> 0 And dictFields.Count = 0 Then
' Some people have reported problems using Request.Form.Count
' Validating a required field instead should work just as well:
'If Request.Form("name") <> "" And dictFields.Count = 0 Then
' Process Input
' This is where you'd actually do something!
' I'm not overly creative, so I just write it out to the browser
%>
Your entry meets our validation criteria!
This would naturally be the point where your just entered data
would be getting logged to a file, inserted into a database, mailed
off to someone, or whatever your plans for it might happen to be!
Since we're just playing with the form here, I simply show it below.
Here's what you entered:
<%
' Loop through the fields writing out the field name and value.
For Each Field In Request.Form
Response.Write Field & ": " & Request.Form(Field) & " " & vbCrLf
Next 'Field
Else
' Two possible reasons for us to be here
' 1. It's their first visit to the page - show the form with no check marks.
' 2. They've entered data and validation failed - show why in form below!
' If they entered some data, and we're still here, then
' they must have made a mistake. Tell them so:
If Request.Form.Count <> 0 Then
%>
I'm sorry but your form wasn't filled out correctly.
Please correct the fields indicated by the check marks.
<%
End If
' Show thw form with checks if appropriate
%>
<%
End If
' Just to make life a little easier for our visitors, we've added two buttons.
' One's attached to each script below. The first simulates a user typing in
' information which fails to meet our criteria in a couple of places. The second
' simulates data which, as far as we're concerned, is fine for whatever
' processing we decide to do to it.
%>