Материал из .Net Framework эксперт
Validate a form
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Validators" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="JavaScript">
function EmpIDClientValidate(ctl, args)
{
args.IsValid=(args.Value%5 == 0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
Name:<asp:TextBox runat="server" Width="200px" ID="Name" />
<asp:RequiredFieldValidator runat="server"
ID="ValidateName"
ControlToValidate="Name"
ErrorMessage="Name is required"
Display="dynamic">*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server"
ID="ValidateName2"
ControlToValidate="Name"
validationExpression="[a-z A-Z]*"
ErrorMessage="Name cannot contain digits"
Display="dynamic">*
</asp:RegularExpressionValidator>
ID (multiple of 5):
<asp:TextBox runat="server" Width="200px" ID="EmpID" />
<asp:RequiredFieldValidator runat="server"
ID="ValidateEmpID"
ControlToValidate="EmpID"
ErrorMessage="ID is required"
Display="dynamic">*
</asp:RequiredFieldValidator>
<asp:CustomValidator runat="server"
ID="ValidateEmpID2"
ControlToValidate="EmpID"
ClientValidationFunction="EmpIDClientValidate"
ErrorMessage="ID must be a multiple of 5"
Display="dynamic"
OnServerValidate="ValidateEmpID2_ServerValidate">*
</asp:CustomValidator>
Day off:<small>08/08/08-08/20/08</small>
<asp:TextBox runat="server" Width="200px" ID="DayOff" />
<asp:RequiredFieldValidator runat="server"
ID="ValidateDayOff"
ControlToValidate="DayOff"
ErrorMessage="Day Off is required"
Display="dynamic">*
</asp:RequiredFieldValidator>
<asp:RangeValidator runat="server"
ID="ValidateDayOff2"
ControlToValidate="DayOff"
MinimumValue="08/08/2008"
MaximumValue="08/20/2008"
Type="Date"
ErrorMessage="Day Off is not within the valid interval"
Display="dynamic"
SetFocusOnError="True">*
</asp:RangeValidator>
Age <small>( >= 18 )</small>:</td>
<asp:TextBox runat="server" Width="200px" ID="Age" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="Age"
ErrorMessage="Age is required"
Display="dynamic"
ID="Requiredfieldvalidator1"
Name="Requiredfieldvalidator1">*
</asp:RequiredFieldValidator>
<asp:CompareValidator runat="server"
ID="ValidateAge"
ControlToValidate="Age"
ValueToCompare="18"
Type="Integer"
Operator="GreaterThanEqual"
ErrorMessage="You must be at least 18-year-old"
Display="dynamic">*
</asp:CompareValidator>
E-mail:
<asp:TextBox runat="server" Width="200px" ID="Email" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="Email"
ErrorMessage="E-mail is required"
Display="dynamic"
ID="Requiredfieldvalidator2"
Name="Requiredfieldvalidator2">*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server"
ID="ValidateEmail"
ControlToValidate="Email"
validationExpression=".*@.{2,}\..{2,}"
ErrorMessage="E-mail is not in a valid format" Display="dynamic">*
</asp:RegularExpressionValidator>
Password:
<asp:TextBox TextMode="Password" runat="server" Width="200px" ID="Password" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="Password"
ErrorMessage="Password is required"
Display="dynamic"
ID="Requiredfieldvalidator3"
Name="Requiredfieldvalidator3">
<img src="http://www.nfex.ru/style/logo.png" border="0">
</asp:RequiredFieldValidator>
Re-enter Password:
<asp:TextBox runat="server" TextMode="Password" Width="200px" ID="Password2" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="Password2"
ErrorMessage="Password2 is required"
Display="dynamic"
ID="Requiredfieldvalidator4"
Name="Requiredfieldvalidator4">
<img src="http://www.nfex.ru/style/logo.png" border="0">
</asp:RequiredFieldValidator>
<asp:CompareValidator runat="server"
ControlToValidate="Password2"
ControlToCompare="Password"
Type="String"
ErrorMessage="The passwords don"t match"
Display="dynamic"
ID="Comparevalidator1"
Name="Comparevalidator1">
<img src="imgError.gif" border="0">
</asp:CompareValidator>
<asp:Button runat="server" Text="Submit" ID="Submit" OnClick="Submit_Click" /><BR>
<asp:CheckBox runat="server" ID="EnableValidators" Checked="True" AutoPostBack="True" Text="Validators enabled" OnCheckedChanged="OptionsChanged" />
<asp:CheckBox runat="server" ID="EnableClientSide" Checked="True" AutoPostBack="True" Text="Client-side validation enabled" OnCheckedChanged="OptionsChanged" />
<asp:CheckBox runat="server" ID="ShowSummary" Checked="True" AutoPostBack="True" Text="Show summary" OnCheckedChanged="OptionsChanged" />
<asp:CheckBox runat="server" ID="ShowMsgBox" Checked="False" AutoPostBack="True" Text="Show message box" OnCheckedChanged="OptionsChanged" />
<asp:ValidationSummary runat="server" ID="ValidationSum" DisplayMode="BulletList" HeaderText="<b>Please review the following errors:</b>"
ShowSummary="true" />
<asp:Label runat="server" ID="Result" ForeColor="magenta" Font-Bold="true" EnableViewState="False" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Validators : System.Web.UI.Page
{
protected void Submit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
Result.Text = "Thanks for sending your data";
else
Result.Text = "There are some errors, please correct them and re-send the form.";
}
protected void OptionsChanged(object sender, EventArgs e)
{
foreach (BaseValidator valCtl in Page.Validators)
{
valCtl.Enabled = EnableValidators.Checked;
valCtl.EnableClientScript = EnableClientSide.Checked;
}
ValidationSum.ShowMessageBox = ShowMsgBox.Checked;
ValidationSum.ShowSummary = ShowSummary.Checked;
}
protected void ValidateEmpID2_ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
args.IsValid = (int.Parse(args.Value) % 5 == 0);
}
catch
{
args.IsValid = false;
}
}
}