ASP.Net/Validation by Control/RangeValidator — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:51, 26 мая 2010
Содержание
RangeValidator Demo: The Quantity field must be from 1 to 30! (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="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>
<BR><BR>
<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>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>
Rangevalidator: Enter an integer between 0 and 100 (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">
CustomValidator Control:
<br><br>
Enter a 4-digit year
</asp:tablecell>
<asp:tablecell runat="server">
<asp:textbox id="year" runat="server"/><br>
<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:
<br><br>
Enter an integer between 0 and 100
</asp:tablecell>
<asp:tablecell runat="server">
<asp:textbox id="value" runat="server"/><br>
<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>
Range validator for Date type value
<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"
/><br />
<b><asp:label runat="server" id="message" /></b>
<br /><asp:button type="submit" runat="server" Text="Submit" />
</form>
RangeValidator for integer type value
<%@ 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">
<div>
<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>
</div>
</form>
</body>
</html>
Set Focus On Error
<%@ 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">
<div>
<asp:Label ID="Label4" runat="server" Text="The Following Field (TextBox4) Must be an Integer Between 1 and 10"></asp:Label><br />
<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><br />
</div>
</form>
</body>
</html>
Validation test: range (C#)
<%@ Page language="c#" src="ValidationTest.aspx.cs" AutoEventWireup="false" Inherits="ValidationTest" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<P>
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><BR>
<BR>
Not validated:
<asp:TextBox id="txtNotValidated" runat="server"></asp:TextBox><BR>
</P>
<P><BR>
<asp:Button id="cmdOK" runat="server" Text="OK"></asp:Button><BR>
<BR>
<asp:Label id="lblMessage" runat="server" EnableViewState="False"></asp:Label></P>
</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.";
}
}
--%>