![]() ![]() | ||
As discussed in the In Depth section of this chapter, validation controls, also called validators, let you check the data a user has entered in a Web application. We've discussed the validation controls available in the In Depth section of this chapter; you can see them all at work in the ValidationControls example on the CD-ROM, which appears in Figure 18.1. This example is a mock-up of a Web application that lets users apply to college, letting them enter data for their class rank, class size, and so on—and checking that data before sending it to the server.
Here is WebForm1.aspx for the ValidationControls example, for reference:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="ValidationControls.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>ValidationControls example</title> <meta content="Microsoft Visual Studio.NET 7.0" name=GENERATOR> <meta content="Visual Basic 7.0" name=CODE_LANGUAGE> <meta content=JavaScript name=vs_defaultClientScript> <meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema> <script language=vbscript> Sub Validate(source, arguments) If (arguments.Value > 20000) Then arguments.IsValid = True Else arguments.IsValid = False End If End Sub </script> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:RequiredFieldValidator id=RequiredFieldValidator1 style="Z-INDEX: 101; LEFT: 235px; POSITION: absolute; TOP: 30px" runat="server" ErrorMessage="You must supply a class rank." ControlToValidate="TextBox1"></asp:RequiredFieldValidator> <asp:Label id=Label5 style="Z-INDEX: 117; LEFT: 73px; POSITION: absolute; TOP: 199px" runat="server">The tuition you'll pay:</asp:Label> <asp:TextBox id=TextBox5 style="Z-INDEX: 116; LEFT: 71px; POSITION: absolute; TOP: 221px" runat="server"></asp:TextBox> <asp:Label id=Label4 style="Z-INDEX: 115; LEFT: 71px; POSITION: absolute; TOP: 152px" runat="server">Your email address:</asp:Label> <asp:Label id=Label3 style="Z-INDEX: 114; LEFT: 69px; POSITION: absolute; TOP: 104px" runat="server">Your age:</asp:Label> <asp:Label id=Label2 style="Z-INDEX: 113; LEFT: 68px; POSITION: absolute; TOP: 56px" runat="server">Your class size:</asp:Label> <asp:Label id=Label1 style="Z-INDEX: 112; LEFT: 66px; POSITION: absolute; TOP: 7px" runat="server">Your class rank:</asp:Label> <asp:TextBox id=TextBox4 style="Z-INDEX: 111; LEFT: 70px; POSITION: absolute; TOP: 175px" runat="server"></asp:TextBox> <asp:TextBox id=TextBox3 style="Z-INDEX: 110; LEFT: 69px; POSITION: absolute; TOP: 124px" runat="server"></asp:TextBox> <asp:TextBox id=TextBox2 style="Z-INDEX: 109; LEFT: 68px; POSITION: absolute; TOP: 78px" runat="server"></asp:TextBox> <asp:Button id=Button1 style="Z-INDEX: 108; LEFT: 62px; POSITION: absolute; TOP: 252px" runat="server" Text="Admit me to college!"> </asp:Button> <asp:TextBox id=TextBox1 style="Z-INDEX: 107; LEFT: 69px; POSITION: absolute; TOP: 25px" runat="server"></asp:TextBox> <asp:ValidationSummary id=ValidationSummary1 style="Z-INDEX: 106; LEFT: 224px; POSITION: absolute; TOP: 245px" runat="server" Width="149px" Height="49px"></asp:ValidationSummary> <asp:CustomValidator id=CustomValidator1 style="Z-INDEX: 105; LEFT: 247px; POSITION: absolute; TOP: 222px" runat="server" ErrorMessage= "You have to pay more than that!" ControlToValidate="TextBox5" ClientValidationFunction="Validate"></asp:CustomValidator> <asp:RegularExpressionValidator id=RegularExpressionValidator1 style="Z-INDEX: 104; LEFT: 242px; POSITION: absolute; TOP: 174px" runat="server" ErrorMessage="Please enter a valid email address." ControlToValidate="TextBox4" ValidationExpression= "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator> <asp:RangeValidator id=RangeValidator1 style="Z-INDEX: 103; LEFT: 242px; POSITION: absolute; TOP: 127px" runat="server" ErrorMessage="You must be at least 18." ControlToValidate="TextBox3" MinimumValue="18" MaximumValue= "150" Type="Integer"></asp:RangeValidator> <asp:CompareValidator id=CompareValidator1 style="Z-INDEX: 102; LEFT: 238px; POSITION: absolute; TOP: 78px" runat="server" ErrorMessage= "Class rank must be less than class size." ControlToValidate="TextBox2" ControlToCompare="TextBox1" Operator="GreaterThanEqual" Width="235px" Height="26px" Type="Integer"></asp:CompareValidator> </form> </body> </HTML>
And here is WebForm1.aspx.vb for the ValidationControls example:
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents RequiredFieldValidator1 As _ System.Web.UI.WebControls.RequiredFieldValidator Protected WithEvents CompareValidator1 As _ System.Web.UI.WebControls.CompareValidator Protected WithEvents RangeValidator1 As _ System.Web.UI.WebControls.RangeValidator Protected WithEvents RegularExpressionValidator1 As _ System.Web.UI.WebControls.RegularExpressionValidator Protected WithEvents CustomValidator1 As _ System.Web.UI.WebControls.CustomValidator Protected WithEvents ValidationSummary1 As _ System.Web.UI.WebControls.ValidationSummary Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox4 As System.Web.UI.WebControls.TextBox Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Label3 As System.Web.UI.WebControls.Label Protected WithEvents Label4 As System.Web.UI.WebControls.Label Protected WithEvents TextBox5 As System.Web.UI.WebControls.TextBox Protected WithEvents Label5 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub End Class
To see how to use the various validators in this program, take a look at the following topics.
![]() ![]() | ||