Input Validation With ASP.NET, Part 1 - Pizza Power
(Page 5 of 6 )
So far, I have demonstrated the RequiredFieldValidator validation control. Now, take a look at this next example, which introduces the next member of the ASP.NET Validator family:
<%@ Page Language="C#" %>
<html>
<head>
<title>Great Pizza Offer</title>
<basefont face="Arial">
</style>
</head>
<body>
<h2>The Great Pizza Offer</h2>
<form runat="server" method="POST" >
<asp:label id="lblPizzaName" runat="server" text="Which Pizza?" />
<asp:textbox id="strPizzaName" runat="server"/>
<asp:RequiredFieldValidator id="strPizzaNameRFV"
ControlToValidate="strPizzaName" ErrorMessage="You need to tell me which pizza you want!" runat="server" Display="dynamic" />
<p> </p>
<asp:label id="lblPizzaQuantity" runat="server" text="How many?" />
<asp:textbox id="strPizzaQuantity" runat="server" />
<asp:RequiredFieldValidator id="strPizzaQuantityRFV"
ControlToValidate="strPizzaQuantity" ErrorMessage="You need to tell me how many you want!" runat="server" Display="dynamic" />
<asp:RangeValidator id="strPizzaQuantityRV"
ControlToValidate="strPizzaQuantity" ErrorMessage="You can select a maximum of three pizzas under this offer!" Type="Integer" MinimumValue="1"
MaximumValue="3" runat="server" Display="dynamic"/>
<p>
<asp:button id="Submit" Text="Order" runat="server"/>
</form>
</body>
</html>
Hit the "Order" button without filling up the form correctly, and a whole bunch of error messages will be thrown back at you.
Let's look at how I managed this feat:
<%
<asp:label id="lblPizzaName" runat="server" text="Which Pizza?" />
<asp:textbox id="strPizzaName" runat="server"/>
<asp:RequiredFieldValidator id="strPizzaNameRFV"
ControlToValidate="strPizzaName" ErrorMessage="You need to tell me which pizza you want!" runat="server" Display="dynamic" />
%>
Nothing new here: I've used the RequiredFieldValidator control again to ensure that the user enters the name of the pizza she wishes to order.
<%
<asp:label id="lblPizzaQuantity" runat="server" text="How many?" />
<asp:textbox id="strPizzaQuantity" runat="server" />
<asp:RequiredFieldValidator id="strPizzaQuantityRFV"
ControlToValidate="strPizzaQuantity" ErrorMessage="You need to tell me how many you want!" runat="server" Display="dynamic" />
<asp:RangeValidator id="strPizzaQuantityRV"
ControlToValidate="strPizzaQuantity" ErrorMessage="You can select a maximum of three pizzas under this offer!" Type="Integer" MinimumValue="1"
MaximumValue="3" runat="server" Display="dynamic"/>
%>
Now, here's something new: a RangeValidator control. As noted earlier, this allows you to specify a range of valid values for the ASP.NET control it is associated with. You've already seen what the id, ControlToValidate, and ErrorMessage attributes do; the RangeValidator control adds three new ones - Type, MinimumValue, and MaximumValue - that allow you to specify the datatype and boundary entries for the range of allowed values. In this case, the RangeValidator control will force the user to enter a number between 1 and 3.
One more interesting thing about the example above: ASP.NET allows you to easily associate two Validator controls with the same server control. For example, in the example above, I have applied the RequiredFieldValidator and the RangeValidator controls to the same strPizzaQuantity text box control. The first forces the user to enter some value in the field; the second sets the allowed values for the field. This capability means that you can easily use a set of validation controls to enforce complex business rules.
Next: A Comparative Study >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire