A Final Look at the Code - Page 7
September 5, 2000
This article has been long and probably a little tough for some of you. If
that is the case, I strongly recommend you go try this all on your own for
a while. When you get frustrated, start to see how the error messages work,
and can debug it pretty well on your own; come back here and re-read this
article and you will find that it makes much more sense the second time
through. Also, if you get horribly stuck, you can always
email me. I cannot guarantee a
reply, but I like to help! For those of you who like to see the code in its
entirety, here it is for your viewing pleasure. Note: This code is
the code that checks specific fields using Regular Expressions, not all
fields are validated, but they could be if we wanted to.
<%
const numFields = 9
dim errorArray()
redim preserve errorArray(numFields)
if request.form("isSubmitted") = "yes" then
first_name = request.form("first_name")
last_name = request.form("last_name")
social_security = request.form("social_security")
address = request.form("address")
city = request.form("city")
state = request.form("state")
zip_code = request.form("zip_code")
telephone = request.form("telephone")
email = request.form("email")
ErrorMsg = ""
dim re, results
set re = New RegExp
'First Name
re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$"
re.Global = True
re.IgnoreCase = True
results = re.Test(first_name)
if results then
errorArray(0) = "False"
else
errorArray(0) = "True"
ErrorMsg = "First Name<br>"
end if
'Last Name
if Len(last_name) = 0 then
errorArray(1) = "True"
ErrorMsg = ErrorMsg & "Last Name<br>"
end if
'SS #
re.Pattern = "^\d{3}-\d{2}-\d{4}$"
results = re.Test(social_security)
if results then
errorArray(2) = "False"
else
errorArray(2) = "True"
ErrorMsg = ErrorMsg & "Social Security<br>"
end if
'Zip Code
re.Pattern = "^\d{5}(-\d{4})?$"
results = re.Test(zip_code)
if results then
errorArray(6) = "False"
else
errorArray(6) = "True"
ErrorMsg = ErrorMsg & "Zip Code<br>"
end if
'Telephone
re.Pattern = "^\d{3}-\d{4}$"
results = re.Test(telephone)
if results then
errorArray(7) = "False"
else
errorArray(7) = "True"
ErrorMsg = ErrorMsg & "Telephone Number<br>"
end if
'Email
re.Pattern = "^\w+@\w+\.\w+"
results = re.Test(email)
if results then
errorArray(8) = "False"
else
errorArray(8) = "True"
ErrorMsg = ErrorMsg & "Email Address<br>"
end if
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Form Field Validation Sample</title>
</head>
<body>
<% if ErrorMsg <> "" then %>
<font color="red" size="3"><b>
<%= ErrorMsg %>
</font><br>
<% end if %>
<form name="sample1" method="post">
<table width="50%" cellspacing="1" cellpadding="0" border="0">
<tr>
<td><% if errorArray(0) = "True" then %>
<font color="red"><b>
<% end if %>First Name:
<% if errorArray(0) = "True" then %>
</b></font>
<% end if %></td>
<td><input name="first_name"
size="25"
maxlength="50"
value="<%= first_name %>"></td>
</tr><tr>
<td><% if errorArray(1) = "True" then %>
<font color="red"><b>
<% end if %>Last Name:
<% if errorArray(1) = "True" then %>
</b></font>
<% end if %></td>
<td><input name="last_name"
size="25"
maxlength="50"
value="<%= last_name %>"></td>
</tr><tr>
<td><% if errorArray(2) = "True" then %>
<font color="red"><b>
<% end if %>Social Security:
<% if errorArray(2) = "True" then %>
</b></font>
<% end if %></td>
<td><input name="social_security"
size="12"
maxlength="12"
value="<%= social_security %>"></td>
</tr><tr>
<td>Address:</td>
<td><input name="address"
size="35"
maxlength="100"
value="<%= address %>"></td>
</tr><tr>
<td>City:</td>
<td><input name="city"
size="25"
maxlength="50"
value="<%= city %>"></td>
</tr><tr>
<td>State: </td>
<td>
<select name="state">
<option value="noneSelected">- Select from List -</option>
<option value="Alabama"
<% if state="Alabama" then response.write("SELECTED") %>>
Alabama - AL</option>
<option value="Alaska"
<% if state="Alaska" then response.write("SELECTED") %>>
Alaska - AK</option>
<option value="Arizona"
<% if state="Arizona" then response.write("SELECTED") %>>
Arizona - AZ</option>
<option value="Arkansas"
<% if state="Arkansas" then response.write("SELECTED") %>>
Arkansas - AR</option>
<option value="California"
<% if state="California" then response.write("SELECTED") %>>
California - CA</option>
<option value="Colorado"
<% if state="Colorado" then response.write("SELECTED") %>>
Colorado - CO</option>
<option value="Connecticut"
<% if state="Connecticut" then response.write("SELECTED") %>>
Connecticut - CT</option>
<option value="Delaware"
<% if state="Delaware" then response.write("SELECTED") %>>
Delaware - DE</option>
<option value="District of Columbia"
<% if state="District of Columbia" then response.write("SELECTED") %>>
District of Columbia - DC</option>
<option value="Florida"
<% if state="Florida" then response.write("SELECTED") %>>
Florida - FL</option>
<option value="Georgia"
<% if state="Georgia" then response.write("SELECTED") %>>
Georgia - GA</option>
<option value="Hawaii"
<% if state="Hawaii" then response.write("SELECTED") %>>
Hawaii - HI</option>
<option value="Idaho"
<% if state="Idaho" then response.write("SELECTED") %>>
Idaho - ID</option>
<option value="Illinois"
<% if state="Illinois" then response.write("SELECTED") %>>
Illinois - IL</option>
<option value="Indiana"
<% if state="Indiana" then response.write("SELECTED") %>>
Indiana - IN</option>
<option value="Iowa"
<% if state="Iowa" then response.write("SELECTED") %>>
Iowa - IA</option>
<option value="Kansas"
<% if state="Kansas" then response.write("SELECTED") %>>
Kansas - KS</option>
<option value="Kentucky"
<% if state="Kentucky" then response.write("SELECTED") %>>
Kentucky - KY</option>
<option value="Louisiana"
<% if state="Louisiana" then response.write("SELECTED") %>>
Louisiana - LA</option>
<option value="Maine"
<% if state="Maine" then response.write("SELECTED") %>>
Maine - ME</option>
<option value="Maryland"
<% if state="Maryland" then response.write("SELECTED") %>>
Maryland - MD</option>
<option value="Massachusetts"
<% if state="Massachusetts" then response.write("SELECTED") %>>
Massachusetts - MA</option>
<option value="Michigan"
<% if state="Michigan" then response.write("SELECTED") %>>
Michigan - MI</option>
<option value="Minnesota"
<% if state="Minnesota" then response.write("SELECTED") %>>
Minnesota - MN</option>
<option value="Mississippi"
<% if state="Mississippi" then response.write("SELECTED") %>>
Mississippi - MS</option>
<option value="Missouri"
<% if state="Missouri" then response.write("SELECTED") %>>
Missouri - MO</option>
<option value="Montana"
<% if state="Montana" then response.write("SELECTED") %>>
Montana - MT</option>
<option value="Nebraska"
<% if state="Nebraska" then response.write("SELECTED") %>>
Nebraska - NE</option>
<option value="Nevada"
<% if state="Nevada" then response.write("SELECTED") %>>
Nevada - NV</option>
<option value="New Hampshire"
<% if state="New Hampshire" then response.write("SELECTED") %>>
New Hampshire - NH</option>
<option value="New Jersey"
<% if state="New Jersey" then response.write("SELECTED") %>>
New Jersey - NJ</option>
<option value="New Mexico"
<% if state="New Mexico" then response.write("SELECTED") %>>
New Mexico - NM</option>
<option value="New York"
<% if state="New York" then response.write("SELECTED") %>>
New York - NY</option>
<option value="North Carolina"
<% if state="North Carolina" then response.write("SELECTED") %>>
North Carolina - NC</option>
<option value="North Dakota"
<% if state="North Dakota" then response.write("SELECTED") %>>
North Dakota - ND</option>
<option value="Ohio"
<% if state="Ohio" then response.write("SELECTED") %>>
Ohio - OH</option>
<option value="Oklahoma"
<% if state="Oklahoma" then response.write("SELECTED") %>>
Oklahoma - OK</option>
<option value="Oregon"
<% if state="Oregon" then response.write("SELECTED") %>>
Oregon - OR</option>
<option value="Pennsylvania"
<% if state="Pennsylvania" then response.write("SELECTED") %>>
Pennsylvania - PA</option>
<option value="Rhode Island"
<% if state="Rhode Island" then response.write("SELECTED") %>>
Rhode Island - RI</option>
<option value="South Carolina"
<% if state="South Carolina" then response.write("SELECTED") %>>
South Carolina - SC</option>
<option value="South Dakota"
<% if state="South Dakota" then response.write("SELECTED") %>>
South Dakota - SD</option>
<option value="Tennessee"
<% if state="Tennessee" then response.write("SELECTED") %>>
Tennessee - TN</option>
<option value="Texas"
<% if state="Texas" then response.write("SELECTED") %>>
Texas - TX</option>
<option value="Utah"
<% if state="Utah" then response.write("SELECTED") %>>
Utah - UT</option>
<option value="Vermont"
<% if state="Vermont" then response.write("SELECTED") %>>
Vermont - VT</option>
<option value="Virginia"
<% if state="Virginia" then response.write("SELECTED") %>>
Virginia - VA</option>
<option value="Washington"
<% if state="Washington" then response.write("SELECTED") %>>
Washington - WA</option>
<option value="West Virginia"
<% if state="West Virginia" then response.write("SELECTED") %>>
West Virginia - WV</option>
<option value="Wisconsin"
<% if state="Wisconsin" then response.write("SELECTED") %>>
isconsin - WI</option>
<option value="Wyoming"
<% if state="Wyoming" then response.write("SELECTED") %>>
Wyoming - WY</option>
</select>
</td>
</tr><tr>
<td><% if errorArray(6) = "True" then %>
<font color="red"><b>
<% end if %>Zip Code:
<% if errorArray(6) = "True" then %>
</b></font>
<% end if %></td>
<td><input name="zip_code" value="<%= zip_code %>"></td>
</tr><tr>
<td><% if errorArray(7) = "True" then %>
<font color="red"><b>
<% end if %>Telephone No.:
<% if errorArray(7) = "True" then %>
</b></font>
<% end if %></td>
<td><input name="telephone" value="<%= telephone %>"></td>
</tr><tr>
<td><% if errorArray(8) = "True" then %>
<font color="red"><b>
<% end if %>Email Address:
<% if errorArray(8) = "True" then %>
</b></font>
<% end if %></td>
<td><input name="email" value="<%= email %>"></td>
</tr><tr>
<td colspan="2">
<input type="submit" value="Submit">
<input type="hidden" name="isSubmitted" value="yes">
</td>
</tr>
</table>
</form>
</body>
</html>
Conclusion
In this article we discussed using Microsoft's Active Server Page
technology to validate user-submitted form data. With this knowledge,
which includes Regular Expressions, for...each looping,
string manipulation, and adding simple memory to forms, you are well on your
way to creating better web pages. The potential uses for this particular
set of technological understanding are limitless, particularly in an
e-Business/Commerce environment!
How To Deal With Specific Sequences of Numbers and Characters - Page 6
Using ASP for Form Handling: Part 2 - Server Side Form-Field Validation
|