|
Form
Validation & Re-display Process
For any forms to fills out, you'll need
to validate the contents and if the contents aren't valid then it is
necessary to re-display the form with an error messages asking the user
to re-enter the missing or invalid data. Finally, you're likely to want
to store data in database or file, or use them to generate another page.
<%@ Language=VBScript %>
<% option explicit %>
<html>
<head>
<title>User Demographic Information Form</title>
</head>
<body>
<%
dim firstname
dim lastname
dim address1
dim address2
dim city
dim state
dim zip
dim getNews
dim newsletter
dim notes
dim i
dim errorMsg
errorMsg=vbnullstring
if Request.ServerVariables("REQUEST METHOD") = "POST" then
firstname = Request.Form("firstname`)
lastname = Request.Form("lastname")
address1 = Request.Form("address1")
address2 = Request.Form("address2")
city = Request.Form("city")
state = Request.Form("state")
zip = Request.Form("zip")
getNews = Request.Form("getNews")
newsletter = Request.Form("newsletter")
notes = Request.Form("notes")
firstname = trim(firstname)
lastname = trim(lastname)
address1 = trim(address1)
address2 = trim(address2)
city = trim(city)
state = trim(state)
zip = trim(zip)
if len(lastName) = 0 then
errorMsg - "You must enter your last name."
else
for i = 1 to len(lastName)
if instr(1, "abcdefghijklmnopqrstuvwxyz",
mid(lastName,i,1), vbTextCompare) = 0 then
errorMsg-"The last name you entered is " & "invalid. Please re-enter your last
name."
exit for
end if
next
end if
if len(errorMsg) = 0 then
if len(firstName) = 0 then
errorMsg="You must enter your first name." else
for i = 1 to len(firstName)
if instr(1, "abcdefghijklmnopqrstuvwxyz",
mid(firstName,i,1), vbTextCompare) = 0 then
errorMsg="The first name you entered " & "is invalid. Please re-enter your "
& "first name."
exit for
end if
next
end if
end if
if len(errorMsg) = 0 then if len(address1) = 0 then
errorMsg="You must enter your address."
else
can't check addresses for validity easily
end if
end if
if len(errorMsg) = 0 then
if len(city) = 0 then
errorMsg="You must enter the city in which you live."
else
for i = 1 to len(city)
if instr(1, "abcdefghijklmnopqrstuvwxyz",mid(city,i,1),
vbTextCompare) = 0 then
errorMsg="The city you entered is invalid. Please re-enter the name of the city in which you live."
exit for
end if
next
end if
end if
if len(errorMsg) = 0 then if len(zip) = 0 then
errorMsg="You must enter your zip code."
elseif len(zip) < 5 then
errorMsg="The zip code you have entered is invalid. Please " & "re-enter the zip code."
elseif len(zip) > 5 and instr(zip,"-")= 0 then
errorMsg="The zip code you entered is invalid. Please " & "re-enter the zip code."
elseif len(zip) > 12 then
errorMsg="The zip code you entered is invalid. Please " & "re-enter the zip code."
else
for i = 1 to len(zip)
if instr(1, "0123456789-",mid(zip,i,1),
vbBinaryCompare) = 0 then
errorMsg-"The zip code you entered 'is invalid. " & "Please re-enter the zip code."
exit for
end if
next
end if
end if
if len(errorMsg) = 0 then if len(getNews) > 0 then
select case newsletter
case "Daily", "Weekly", "Monthly"
ok
case else
errorMsg="Select how often you wish to receive our " & "newsletter via email."
end select
end if
end if
if len(errorMsg) = 0 then
Response.Write "FirstName=" & firstName & "<br>"
Response.Write "lastName=" & lastname & "<br>"
Response.Write "Address1=" & address1 & "<br>"
Response.Write "Address2=" & address2 & "<br>"
Response.Write "City=" & city & "<br>"
Response.Write "State=" & state & "<br>"
Response.Write "Zip=" & zip & "<br>"
Response.Write "Receive Newsletter=",& getNews & "<br>"
Response.Write "Newsletter Schedule=" & newsletter & "<br>"
Response.Write "Notes=" & notes
Response.End
end if
end if
%>
<hr>
<P This is an HTML form. It should look familiar. It contains several text boxes and two buttons. All the values are
required. Fill out the form and click the Submit button. To reset the form to the initial values, click the Reset button.</p>
<p><b>Enter your information, then click Submit</b></p>
<p>
<form name="frmUserInfo" action="tt1.asp" method="post">
<%
if len(errorMsg) > 0 then
Response.Write "<p><font color='red'>" & errorMsg & "</font></p>"
end if
<table border="1" cellPadding="1" cellSpacing="1" width="75%">
<tr>
<td width="30%"><b>First Name</b>
</td>
<td colspan="2">
<b><input type="text" name="FirstName" value="<%=firstname%>"></b></td>
</tr>
<tr>
<td><b>Last Name</b> </td>
<td colspan="2"><input type="text"
name="LastName" value="<%=lastname%>"></td> </tr>
<tr>
<td><b>Address1</b></td>
<td colspan=2><input type="text" name="address1" value="<%=address1%>"</td> </tr>
<tr>
<td><b>Address2</b></td>
<td colspan=2><input type="text" name="address2"
value="<%=address2%>"></td></tr>
<tr>
<td><b>City</b></td>
<td colspan=2><input type="text" name="city"
value="<%=city%>"></td></tr>
<tr>
<td><b>State</b></td>
<td colspan=2><select name="State" width="165">
<option value=AL>AL</option><option value=AK>AK</option>
<option value=AR>AR</option><option value=AZ>AZ</option>
<option value=CA>CA</option><option value=CT>CT</option>
<option value=DE>DE</option><option value=FL>FL</option>
<option value-GA>GA</option><option value=HI>HI</option>
<option value=IA>IA</option><option value=ID>ID</option>
<option value=IL>IL</option><option value=IN>IN</option>
<option value=KS>KS</option><option value=KY>KY </option>
<option value=LA>LA</option><option value=MA>MA </option>
<option value=lD>MD</option><option value=ME>ME </option>
<option value=MI>MI</option><option value=MN>MN </option>
<option value=MO>MO</option><option value=MS>MS </option>
<option value-MT>MT</option><option value=NE>NE </option>
<option value=NH>NH</option><option value=NC>NC </option>
<option value=ND>ND</option><option value=NJ>NJ </option>
<option value=NM>NM</option><option value=NV>NV </option>
<option value=NY>NY</option><option value=OH>OH </option>
<option value=OK>OK</option><option value=OR>OR </option>
<option value=PA>PA</option><option value=PR>PR </option>
<option value=RI>RI</option><option value=SC>SC </option>
<option value=SD>SD</option><option value=TN>TN </option>
<option value=TX>TX</option><option value=UT>UT </option>
<option value=VT>VT</option><option value=VA>VA </option>
<option value-WA>WA</option><option value=WI>WI </option>
<option value=WV>WV</option><option value-WY>WY </option>
</select>
</td>
</tr>
<tr>
<td>
<b>Zip</b></td> <td colspan=2><input type="text" name="zip" value="<%=zip%>"></td> </tr>
<tr>
<td><b>Receive Newsletter?</b>
</td>
<td colspan=2>
<input type="checkbox" name="getNews" <%if getNews then Response.write
"checked"%>
I would like to receive your newsletter via email.</input>
</td> </tr>
<tr>
<td><b>Newsletter Schedule</b></td> <td
colspan=2>
<input type="radio" name="newsletter" value="Daily"
<%if newsletter="Daily" then Response.write "checked"%>
Daily</input><br>
<input type="radio" name="newsletter"
value="Weekly"
<%if newsletter="Weekly" then Response.write "checked"%> Weekly</input>
<br>
<input type="radio" name="newsletter" value="Monthly"
<%if newsletter="Monthly" then Response.write "checked"%> Monthly</input>
</td>
</tr>
<tr>
<td><b>Notes</b></td>
<td colspan=2><textarea name="notes">Enter any additional notes here.</textarea>
</td>
</tr>
<tr>
<td align=center> </td>
<td align=center width=110><input name="Submit" type=submit value=" Submit "></td>
<td align=center width=100><input name="Reset" type=reset value=" Reset "></td>
</tr>
</table>
</form>
</p>
</body>
</html>
|