ASP.Net/Validation by Control/CompareValidator — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:30, 26 мая 2010
Содержание
asp:CompareValidator Demo (VB.net)
<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<script runat=server>
</SCRIPT>
<HTML>
<HEAD>
<TITLE>CompareValidator Control Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma">
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
Text="Date"
/>
<asp:TextBox
id="txtDateField"
Columns="25"
MaxLength="30"
runat=server
/>
<asp:CompareValidator
id="cvDateField"
ControlToValidate="txtDateField"
Operator="DataTypeCheck"
Type="Date"
Display="Dynamic"
Font-Size="10pt"
runat="server">
You must enter a date!
</asp:CompareValidator>
<BR><BR>
<asp:Label
id="lblMessage2"
runat="server"
Font-Bold="True"
Text="Number"
/>
<asp:TextBox
id="txtNumber"
Columns="25"
MaxLength="30"
runat=server
/>
<asp:CompareValidator
id="cvNumber"
ControlToValidate="txtNumber"
Operator="DataTypeCheck"
Type="Integer"
Display="Dynamic"
runat="server">
You must enter a whole number!
</asp:CompareValidator>
<BR><BR>
<asp:Label
id="lblMessage3"
runat="server"
Font-Bold="True"
Text="Make the same"
/>
<asp:TextBox
id="txtSame1"
Columns="25"
MaxLength="30"
runat=server
/>
<asp:TextBox
id="txtSame2"
Columns="25"
MaxLength="30"
runat=server
/>
<asp:CompareValidator id="cvSame"
ControlToValidate="txtSame1"
ControlToCompare="txtSame2"
Display="Dynamic"
Font-Name="Tahoma"
Font-Size="10pt"
runat=server>
Fields don"t match!
</asp:CompareValidator>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>
Comparevalidator: equal (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<title>Validation Control Example</title>
<script language="javascript">
<!--
function ClientValidate(source, arguments)
{
//alert(arguments.Value);
var r, re; //Declare variables.
re = new RegExp(/^[1-9][0-9][0-9][0-9]$/); //Create regular expression object.
r = re.test(arguments.Value); //Test for match.
arguments.IsValid = r; //Return results.
}
-->
</script>
<script runat="server">
Sub Page_Load()
vsSummary.DisplayMode = ValidationSummaryDisplayMode.List
End Sub
Sub ServerValidation (source As object, args As ServerValidateEventArgs)
Dim RegExVal As New System.Text.RegularExpressions.Regex("^\d{4}$")
If RegExVal.IsMatch(args.Value) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
</script>
</head>
<body>
<h1>Validation Control Example</h1>
<form runat="server">
<asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
<asp:tablerow runat="server">
<asp:tablecell runat="server">
Compare Validator Control:
<br><br>
Enter two numbers to compare
</asp:tablecell>
<asp:tablecell runat="server">
<asp:textbox id="value1" runat="server"/><br>
<asp:textbox id="value2" runat="server"/><br>
<asp:comparevalidator id="cvCompare"
controltovalidate="value1"
controltocompare="value2"
operator="equal"
type="integer"
errormessage="Fields are not equal!"
display="dynamic"
runat="server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
ValidationSummary Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:validationsummary id="vsSummary"
displaymode="bulletlist"
headertext="Page has the following errors: "
showsummary="true"
showmessagebox="false"
runat="server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell colspan="2" runat="server">
<asp:button text="submit" runat="server"/>
</asp:tablecell>
</asp:tablerow>
</asp:table>
<asp:label id="MyLabel" runat="server"/>
</form>
</body>
</html>
Set CompareValidator to Today
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<!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>Default</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label2"
runat="server"
Text="The Following Field Must Be a Date and Earlier than Today"></asp:Label><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1"
runat="server"
ControlToValidate="TextBox2"
ErrorMessage="TextBox2 Must Be a Date and Earlier than Today"
Operator="LessThan"
SetFocusOnError="True"
Type="Date">*</asp:CompareValidator><br />
<br /><br /><br /><br /><br />
The Following Fields Must Contain a Early Date and a Later Date<br />
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text="through"></asp:Label>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator2"
runat="server"
ControlToCompare="TextBox6"
ControlToValidate="TextBox5"
ErrorMessage="TextBox5 Must be Earlier Than TextBox6"
Operator="GreaterThan"
Type="Date">*</asp:CompareValidator><br />
<asp:Button ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Submit" />
<asp:Button ID="Button2"
runat="server"
CausesValidation="False"
Text="Cancel - Will Not Validate!" />
<asp:Button ID="Button3"
runat="server"
Text="Validate DropDownList1 Only"
ValidationGroup="DDL" /><br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" />
<asp:ValidationSummary ID="ValidationSummary2"
runat="server"
ShowMessageBox="True"
ValidationGroup="DDL" />
</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 Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.rupareValidator1.ValueToCompare = DateTime.Today.ToShortDateString();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Validate(string.Empty);
if (this.IsValid==false)
{
// Do something...
}
}
}
Validating User Input Against a Value Through the CompareValidator Control (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Validating User Input Against a Value Through the CompareValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
What is the next generation of ASP called?<BR>
<asp:textbox
id="txtAnswer"
runat=server
/>
<asp:CompareValidator id="cvCheckAnswer"
ControlToValidate="txtAnswer"
ValueToCompare="ASP.NET"
runat=server>
Incorrect Answer!
</asp:CompareValidator>
<BR><BR>
<asp:button
id="butOK"
text="OK"
type="Submit"
runat="server"
/>
</form>
</BODY>
</HTML>
Validating User Input against the Value in Another Control Using the CompareValidator Control (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Validating User Input against the Value in Another Control Using the CompareValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter the same value in both TextBox controls:
<BR>
<asp:textbox
id="txtSame1"
runat=server
/>
<BR>
<asp:textbox
id="txtSame2"
runat=server
/>
<asp:comparevalidator
id="cvCheckValues"
controltovalidate="txtSame1"
controltocompare="txtSame2"
runat=server
>
<BR>Please enter the same value in both boxes.
</asp:comparevalidator>
<BR><BR>
<asp:button
id="butOK"
text="OK"
type="Submit"
runat="server"
/>
</form>
</BODY>
</HTML>