ASP.Net/Validation by Control/RangeValidator
Содержание
RangeValidator Demo: The Quantity field must be from 1 to 30! (VB.net)
<source lang="csharp">
<%@ 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">
<asp:Label
id="lblMessage" runat="server" Font-Bold="True" Text="Quantity"
/> <asp:TextBox
id="txtQuantity" Columns="25" MaxLength="30" runat=server
/> <asp:RangeValidator
id="rngQuantity" ControlToValidate="txtQuantity" Type="Integer" MinimumValue=1 MaximumValue=30 Display="Dynamic" Font-Name="Verdana" Font-Bold="True" Font-Size="10pt" runat="server"> The Quantity field must be from 1 to 30!
</asp:RangeValidator>
<asp:Label
id="lblMessage2" runat="server" Font-Bold="True" Text="Date"
/> <asp:TextBox
id="txtDate" Columns="25" MaxLength="30" runat=server
/> <asp:RangeValidator
id="rngDate" ControlToValidate="txtDate" Type="Date" MinimumValue="1/1/2000" MaximumValue="12/31/2010" Display="Dynamic" Font-Name="Verdana" Font-Bold="True" Font-Size="10pt" runat="server"> The Date field must be from 1/1/2000 to 12/31/2001!
</asp:RangeValidator>
<asp:button
id="butOK" text="OK" Type="Submit" runat="server"
/> </Form> </BODY> </HTML>
</source>
Rangevalidator: Enter an integer between 0 and 100 (VB.net)
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<title>Validation Control Example</title> <script language="javascript"> </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>
Validation Control Example
<form runat="server"> <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server"> <asp:tablerow runat="server"> <asp:tablecell runat="server"> CustomValidator Control:
Enter a 4-digit year </asp:tablecell> <asp:tablecell runat="server"> <asp:textbox id="year" runat="server"/>
<asp:customvalidator id="cvDate" controltovalidate="year" errormessage="Not a valid year!" onservervalidate="servervalidation" clientvalidationfunction="ClientValidate" display="dynamic" runat="server"/> </asp:tablecell> </asp:tablerow> <asp:tablerow runat="server"> <asp:tablecell runat="server"> RangeValidator Control:
Enter an integer between 0 and 100 </asp:tablecell> <asp:tablecell runat="server"> <asp:textbox id="value" runat="server"/>
<asp:rangevalidator id="rvCompare" controltovalidate="value" minimumvalue="0" maximumvalue="100" type="integer" errormessage="Value not in valid range!" 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>
</source>
Range validator for Date type value
<source lang="csharp">
<script runat="server" language="C#" > protected void Page_Load(object o, EventArgs e) {
if(IsPostBack && rangeValidator.IsValid ){ message.Text = "Thanks, see you in September."; } else { message.Text = "Please start vacations on 2008/08/01"; }
} </script> <form runat="server"> Enter your desired vacation start date: <asp:Textbox runat="server" id="theDate" /> <asp:RangeValidator runat="server"
id="rangeValidator" ControlToValidate="theDate" ErrorMessage="vacations must start on August 1st" Type="Date" MinimumValue="2008/08/01" MaximumValue="2008/08/01"
/>
<asp:label runat="server" id="message" />
<asp:button type="submit" runat="server" Text="Submit" />
</form>
</source>
RangeValidator for integer type value
<source lang="csharp">
<%@ Page Language="C#" %> <!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>
</head> <body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="txtValue" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Submit" /> <asp:RangeValidator ControlToValidate="txtValue" ErrorMessage="RangeValidator" ID="RangeValidator1" MaximumValue="20" MinimumValue="10" runat="server" Type="Integer">Between 10 and 20 please</asp:RangeValidator>
</form>
</body> </html>
</source>
Set Focus On Error
<source lang="csharp">
<%@ Page Language="C#" %> <!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>
</head> <body>
<form id="form1" runat="server">
<asp:Label ID="Label4" runat="server" Text="The Following Field (TextBox4) Must be an Integer Between 1 and 10"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox4" ErrorMessage="TextBox4 Must be an Integer Between 1 and 10" MaximumValue="10" MinimumValue="1" SetFocusOnError="True">*</asp:RangeValidator>
</form>
</body> </html>
</source>
Validation test: range (C#)
<source lang="csharp">
<%@ Page language="c#" src="ValidationTest.aspx.cs" AutoEventWireup="false" Inherits="ValidationTest" %> <HTML>
<body> <form id="Form1" method="post" runat="server">
A number (1 to 10):
<asp:TextBox id="txtValidated" runat="server"></asp:TextBox>
<asp:RangeValidator id="RangeValidator" runat="server" ErrorMessage="This Number Is Not In The Range" ControlToValidate="txtValidated" MaximumValue="10" MinimumValue="1" Type="Integer"></asp:RangeValidator>
Not validated:
<asp:TextBox id="txtNotValidated" runat="server"></asp:TextBox>
<asp:Button id="cmdOK" runat="server" Text="OK"></asp:Button>
<asp:Label id="lblMessage" runat="server" EnableViewState="False"></asp:Label>
</form> </body>
</HTML>
<%-- using System; using System.Collections; using System.ruponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public class ValidationTest : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtValidated; protected System.Web.UI.WebControls.RangeValidator RangeValidator; protected System.Web.UI.WebControls.TextBox txtNotValidated; protected System.Web.UI.WebControls.Button cmdOK; protected System.Web.UI.WebControls.Label lblMessage; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void cmdOK_Click(object sender, System.EventArgs e) { if (!this.IsValid) return; lblMessage.Text = "cmdOK_Click event handler executed."; } }
--%>
</source>