ASP.Net/Validation by Control/Required Field
Содержание
- 1 Checking Against an Initial Value with a RequiredFieldValidator Control (VB.net)
- 2 DropDownList Must Have a Value Selected Other Than the Default
- 3 Must enter: Required Field Validator (C#)
- 4 RequiredFieldValidator Demo: You must enter your name (VB.net)
- 5 Required field validator (VB.net)
- 6 Validation for aspText: cannot be empty, integer range (MaximumValue,MinimumValue) (VB.net)
Checking Against an Initial Value with a RequiredFieldValidator Control (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Checking Against an Initial Value with a RequiredFieldValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:textbox
id="txtName"
text="Enter Your Name"
runat=server
/>
<asp:RequiredFieldValidator
id="rfvName1"
controltovalidate="txtName"
InitialValue="Enter Your Name"
display="Dynamic"
font-name="Verdana"
font-size="10pt"
forecolor="red"
runat=server
>
You must enter your name!
</asp:RequiredFieldValidator>
<asp:requiredfieldvalidator
id="rfvName2"
controltovalidate="txtName"
display="Dynamic"
font-name="Verdana"
font-size="10pt"
forecolor="red"
runat=server
>
You must enter your name!
</asp:requiredfieldvalidator>
<BR><BR>
<asp:button
id="butOK"
text="OK"
type="Submit"
runat="server"
/>
</form>
</BODY>
</HTML>
DropDownList Must Have a Value Selected Other Than the 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" ValidationGroup="DDL">
<asp:ListItem>--Please Select--</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="DropDownList1 Must Have a Value Selected"
InitialValue="--Please Select--"
SetFocusOnError="True"
ValidationGroup="DDL">*</asp:RequiredFieldValidator><br />
</div>
</form>
</body>
</html>
Must enter: Required Field Validator (C#)
<%@Page Language="C#"%>
<html>
<body>
<form runat="server">
<asp:textbox id="txtTest" text="delete this" runat="server" />
<asp:RequiredFieldValidator id="valRequired" runat="server"
ErrorMessage="* You must enter a value in the text box"
ControlToValidate="txtTest"
Display="dynamic">
*
</asp:RequiredFieldValidator>
<br />
<asp:button text="Validate" runat="server" />
<asp:ValidationSummary id="valSummary" runat="server"
HeaderText="<b>The following errors were found:</b>"
ShowSummary="true"
DisplayMode="List" />
</form>
</body>
</html>
RequiredFieldValidator Demo: You must enter your name (VB.net)
<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<script runat=server>
</SCRIPT>
<HTML>
<HEAD>
<TITLE>RequiredFieldValidator Control Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma">
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
Text="Name"
/>
<asp:TextBox
id="txtName"
Columns="25"
MaxLength="30"
runat=server
Text="Name"
/>
<asp:RequiredFieldValidator
id="rfvName"
ControlToValidate="txtName"
InitialValue="Name"
Display="Dynamic"
Font-Name="Verdana"
Font-Size="10pt"
runat=server
>
You must enter your name!
</asp:RequiredFieldValidator>
<BR><BR>
<asp:Label
id="lblMessage2"
runat="server"
Font-Bold="True"
Text="Name 2"
/>
<asp:TextBox
id="txtName2"
Columns="25"
MaxLength="30"
runat=server
Text="Name"
/>
<asp:RequiredFieldValidator
id="rfvName2"
ControlToValidate="txtName2"
InitialValue="Name"
Display="Dynamic"
Font-Name="Verdana"
Font-Size="10pt"
runat=server
>
You must replace name with your name!
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator
id="rfvName3"
ControlToValidate="txtName2"
Display="Dynamic"
Font-Name="Verdana"
Font-Size="10pt"
runat=server
>
You must enter a name!
</asp:RequiredFieldValidator>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>
Required field validator (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">
RegularExpressionValidator Control:
<br><br>
Enter a valid 5 or 9-digit zip code
</asp:tablecell>
<asp:tablecell runat="server">
<asp:textbox id="zipcode" runat="server"/><br>
<asp:regularexpressionvalidator id="reZipCode"
controltovalidate="zipcode"
validationexpression="^\d{5}$|^\d{5}-\d{4}$"
errormessage="Not a valid Zip code!"
display="static"
runat="server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
RequiredFieldValidator Control:
<br><br>
Enter a login name
</asp:tablecell>
<asp:tablecell runat="server">
<asp:textbox id="login" runat="server"/><br>
<asp:requiredfieldvalidator id="rfvLogin"
controltovalidate="login"
display="static"
errormessage="Login cannot be blank!"
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>
Validation for aspText: cannot be empty, integer range (MaximumValue,MinimumValue) (VB.net)
<%@ Page Language="VB" %>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Name:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
id="RequiredFieldValidator1"
runat="server"
ErrorMessage="You must enter your name"
ControlToValidate="TextBox1">Missing name
</asp:RequiredFieldValidator>
</p>
<p>
Age:
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:RangeValidator
id="RangeValidator1"
runat="server"
ErrorMessage="Age must be between 18 and 75"
ControlToValidate="TextBox2"
MaximumValue="75"
MinimumValue="18"
Type="Integer">18 - 75
</asp:RangeValidator>
</p>
<p>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</p>
<p>
<asp:ValidationSummary
id="ValidationSummary1"
runat="server">
</asp:ValidationSummary>
</p>
</form>
</body>
</html>