PDA

View Full Version : Make a form field required


twalter
09-14-2006, 01:34 AM
In this example I will show you how to make a form field required before allowing it to be processed. In this example we will use a self submitting form using a case statement.

Dim Variable name '(ShowAction)
ShowAction = Request.QueryString("action")
'QueryString (what shows in the address of bar) exampe.asp?action=show&error="&strerr&""

Key componets of a Case statement

Select Case ShowAction
Case ""
...
'Show Form
...
Case "show"
'process form
...
'end of process
End Select


According to how the fields are entered the following is processed.
if strerr = "1" then
response.write "Please fill in the field!"
end if

if strerr = "2" then
response.write "Please fill in the field!"
end if

if strerr = "3" then
response.write "Please fill in the field!"
end if

When the form is being processed then the following will be applied

if strfname = "" and strlname <> "" then
response.redirect "example.asp?error=1"
end if
if strfname <> "" and strlname = "" then
response.redirect "example.asp?error=2"
end if
if strlname = "" and strfname = "" then
response.redirect "example.asp?error=3"
end if

If both fields are enter then strerr will = blank and then process the rest of the form and give you your results.

Response.write "Hi " & strfname & "" & strlname & "<br>"


Code:
-------------------------------------------------------

<%@Language = VBScript %>
<% Option Explicit %>
<html>
<head>
<title>Required field example</title>
</head>
<body>

<%

'dim your variables
dim ShowAction, strerr, strfname, strlname

ShowAction = request.QueryString("action")
strerr = request.QueryString("error")
strfname = request.form("fname")
strlname = request.form("lname")

'load the form page
Select Case ShowAction
Case ""

'list our error codes and descriptions
if strerr = "1" then
response.write "<font color=""red""><b>Please enter your first name!</b></font>"
end if

if strerr = "2" then
response.write "<font color=""red""><b>Please enter your last name!</b></font>"
end if

if strerr = "3" then
response.write "<font color=""red""><b>Please in both fields!</b></font>"
end if
response.write strfname
%>

<form action="example.asp?action=show&error="&strerr&"" method="post">
<p>
<p>Enter your name: <input type="text" value="<%=Session("fname")%>" name="fname" size="10" maxlength="10"><br>
Enter your last name: <input type="text" value="<%=Session("lname")%>" name="lname" size="30" maxlength="40">
<br>
<table>
<tr><td><input type="submit" value="Submit" name="submit"></form></td><td><form action="example.asp&action=clear" method="post"> <input type="submit" name="reset" value=" Reset"></form></td></tr>
</table>

<p>
<%
'User has hit submit
Case "show"
Session("fname") = strfname
Session("lname") = strlname

if strfname = "" and strlname <> "" then
response.redirect "example.asp?error=1"
end if
if strfname <> "" and strlname = "" then
response.redirect "example.asp?error=2"
end if
if strlname = "" and strfname = "" then
response.redirect "example.asp?error=3"
end if

'Show detaills
Response.write "Hi " & strfname & "" & strlname & "<br>"

case "clear"
Session.Abandon
response.redirect "example.asp"

End Select
%>
</body>
</html>
--------------------------------------

See the demo (http://www.aspbasic.net/examples/new/example.asp)

Download the example (http://www.aspbasic.net/examples/new/example.zip)