Tuesday, January 25, 2011

Validation Controls

Here is the aspx code:

<%@ 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>

<asp:Label ID="Label1" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtLastName" Display="None"
ErrorMessage="Last name is required"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label2" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Phone Number"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPhone" Display="None"
ErrorMessage="Must enter Phone Number"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label4" runat="server" Text="Email Address"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtEmail" Display="None" ErrorMessage="Not a valid email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<br />
<asp:Label ID="Label5" runat="server" Text="Zip code"></asp:Label>
<asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label6" runat="server" Text="Hire Date"></asp:Label>
<asp:TextBox ID="txtHireDate" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="txtHireDate" Display="None"
ErrorMessage="Must be a valid date" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Select a Dept</asp:ListItem>
<asp:ListItem>Accounting</asp:ListItem>
<asp:ListItem>IT</asp:ListItem>
<asp:ListItem>Sales</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label7" runat="server" Text="Annual Salary $"></asp:Label>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="txtSalary" Display="None" ErrorMessage="CompareValidator"
Operator="DataTypeCheck" Type="Currency"></asp:CompareValidator>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</div>
</form>
</body>
</html>

Here is the code from the C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string phone = txtPhone.Text;
if (phone.Length > 10)
{
Response.Write("Only use digits for phone number");
}

if (DropDownList1.SelectedIndex == 0)
{
Response.Write("Choose a Department");
}
}
}

No comments:

Post a Comment