







|
 |


Bringing it all together
So, after all that, what does our page look like? These are the details about our form:
- It has three fields
- The first two are required, the third is not
- The 2nd field has a minimum of 5 characters
- Both the first two fields have a maximum of 50 characters, the 3rd has a maximum of 10 characters
- The third field can only contain numeric characters
So our code will look like this (some whitespace, eg indenting, may have been removed to counter excessive line length. This is too avoid excessive
wrapping in 800x600 displays).
<%@ Language=VBScript %>
<%
Option Explicit
%>
<%
Dim strTxt1
Dim strTxt2
Dim strTxt3
Dim objErrors
Dim errCode
If isPostBack() then
strTxt1 = Request.Form("txt1")
strTxt2 = Request.Form("txt2")
strTxt3 = Request.Form("txt3")
Set objErrors = Server.CreateObject("Scripting.Dictionary")
objErrors.Add "strTxt1-Missing", "You did not enter a value in the 1st field"
objErrors.Add "strTxt2-TooShort", "Please enter at least 5 characters in the 2nd field"
objErrors.Add "strTxt3-TooLong", "Please enter no more than 10 characters in the 3rd field"
objErrors.Add "strTxt3-Invalid", "Please enter only digits (0-9) in the 3rd field"
If not TestMinLength(strTxt1, 1) then errCode = AddUserError(errCode, "strTxt1-Missing")
If not TestMaxLength(strTxt1, 50) then strTxt1 = Left(strTxt1, 50)
If not TestMinLength(strTxt2, 5) then errCode = AddUserError(errCode, "strTxt2-TooShort")
If not TestMaxLength(strTxt2, 50) then strTxt2 = Left(strTxt2, 50)
If not TestMaxLength(strTxt3, 10) then errCode = AddUserError(errCode, "strTxt3-TooLong")
If not IsAcceptableValues(strTxt3, "0123456789", 0, 0) then _
errCode = AddUserError(errCode, "strTxt3-Invalid")
If isEmpty(errCode) then
Else
End If
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Validating User Intput - adOpenStatic.com</title>
<% Call WritePageHeader() %>
</head>
<body>
<%
Call WritePageBody()
If not isEmpty(errCode) then
Call WriteUserErrors(errCode, arrErrors, "An error occurred")
End If
Call WriteFormHeader("Form1Validator")
Call WriteFormFieldReqd("txt1", "1st field")
Call WriteFormFieldReqd("txt2", "2nd field")
Call WriteFormFieldMinLength("txt2", 5, "2nd field")
Call WriteFormFieldAccept("txt3", "0123456789", _
"Please enter only 0-9 values in the 3rd field")
Call WriteFormFooter()
%>
<form method="post"
action="page1.asp" name="Form1"
onSubmit="return Form1Validator(this)">
<input
type="hidden"
name="postback"
value="<% =GetFileName()%>"><br>
1st field: <input
type="text"
name="txt1"
value="<% =strTxt1%>"><br>
2nd field: <input
type="text"
name="txt2"
value="<% =strTxt2%>"><br>
3rd field: <input
type="text"
name="txt3"
value="<% =strTxt3%>"><br>
<input
type="Submit">
</form>
<% Call WritePageFooter() %>
</body>
</html>
Generating the Error | Online Test
Back to Code Listing
|
 |
 |