Tuesday, April 26, 2011

Validation Controls

Here is the default.aspx with the validation controls:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:Label ID="Label1" runat="server" Text="Enter First Name"></asp:Label><asp:TextBox ID="txtFirstName"
runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="*Enter Last Name"></asp:Label><asp:TextBox ID="txtLastName"
runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtLastName" ErrorMessage="Last Name is required"
ForeColor="Red" Display="None"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label3" runat="server" Text="Enter Address"></asp:Label><asp:TextBox ID="txtAddress"
runat="server"></asp:TextBox><br />
<asp:Label ID="Label4" runat="server" Text="Enter City"></asp:Label><asp:TextBox ID="txtCity"
runat="server"></asp:TextBox><br />
<asp:Label ID="Label5" runat="server" Text="Enter state"></asp:Label><asp:TextBox ID="txtState"
runat="server"></asp:TextBox><br />
<asp:Label ID="Label6" runat="server" Text="*Enter Zipcode"></asp:Label><asp:TextBox ID="txtZip"
runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtZip" ErrorMessage="Zip code is required"
ForeColor="Red" Display="None"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtZip" Display="None" ErrorMessage="Invalid Zipcode"
ForeColor="Red" ValidationExpression="\d{5}(-\d{4})?"></asp:RegularExpressionValidator>
<br />
<asp:Label ID="Label7" runat="server" Text="Enter Phone"></asp:Label><asp:TextBox ID="txtPhone"
runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtPhone" Display="None" ErrorMessage="invalid phone format"
ForeColor="Red" ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"></asp:RegularExpressionValidator>
<br />
<asp:Label ID="Label8" runat="server" Text="Enter email"></asp:Label><asp:TextBox ID="txtEmail"
runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ControlToValidate="txtEmail" Display="None" ErrorMessage="Invalid email"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<br />
<asp:Label ID="Label9" runat="server" Text="Enter Age"></asp:Label><asp:TextBox ID="txtAge"
runat="server"></asp:TextBox><br />

<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<asp:Label ID="Label10" runat="server" Text="Label"></asp:Label>
</p>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
</form>
</body>
</html>

Here is the code for our validation of the age text box:

protected void Button1_Click(object sender, EventArgs e)
{
int age;
bool goodAge = int.TryParse(txtAge.Text, out age);

if (goodAge)
{
if (age < 18)
{
Label10.Text = "No way kid!";
}
if (age > 115)
{
Label10.Text = "Really?";
}
}
else{
Label10.Text = "Enter a number for Age";
}
}

No comments:

Post a Comment