Validation Control of Asp.net
Validation Server Control Description
CompareValidator Compares the value of one input control
to the value of another input control or to
a fixed value
CustomValidator Allows you to write a method to handle
the validation of the value entered
RangeValidator Checks that the user enters a value
that falls between two values
RegularExpressionValidator Ensures that the value of an input control
matches a specified pattern
RequiredFieldValidator Makes an input control a required field
ValidationSummary Displays a report of all validation errors
occurred in a Web page
Compare Validations
<asp:CompareValidator id="compval" Display="dynamic" ControlToValidate="txt1" ControlToCompare="txt2" ForeColor="red" BackColor="red" Type="String" EnableClientScript="false"
Text="Validation Failed!" runat="server" />
Custom Validator
<script runat="server">
Sub user(source As object,args As ServerValidateEventArgs)
if len(args.Value)<8 or len(args.Value)>16 then
args.IsValid=false
else
args.IsValid=true
end if
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Label runat="server" Text="Enter a username: " />
<asp:TextBox id="txt1" runat="server" />
<asp:Button Text="Submit" runat="server"/>
<br />
<asp:Label id="mess" runat="server"/>
<br />
<asp:CustomValidator
ControlToValidate="txt1"
OnServerValidate="user"
Text="A username must be between 8 and 16 characters!"
runat="server"/>
</form>
</body>
</html>
---------------------------------------------------------------------------
Range Validator
<asp:RangeValidator ControlToValidate="tbox1" MinimumValue="2005-01-01"
MaximumValue="2005-12-31" Type="Date" EnableClientScript="false"
Text="The date must be between 2005-01-01 and 2005-12-31!" runat="server" />
Regular Expression Validator
<asp:RegularExpressionValidator ControlToValidate="txtbox1" ValidationExpression="\d{5}"
EnableClientScript="false" ErrorMessage="The zip code must be 5 digits!" runat="server" />
RequiredField Validator
<asp:RequiredFieldValidator ControlToValidate="name" Text="The name field is required!"
runat="server" />
Validation Summary
<form runat="server">
<table>
<tr>
<td>
<table bgcolor="#b0c4de" cellspacing="10">
<tr>
<td align="right">Name:</td>
<td><asp:TextBox id="txt_name" runat="server"/></td>
<td>
<asp:RequiredFieldValidator
ControlToValidate="txt_name"
ErrorMessage="Name"
Text="*"
runat="server"/>
</td>
</tr>
<tr>
<td align="right">Card Type:</td>
<td>
<asp:RadioButtonList id="rlist_type"
RepeatLayout="Flow"
runat="server">
<asp:ListItem>Diners</asp:ListItem>
<asp:ListItem>MasterCard</asp:ListItem>
<asp:ListItem>Visa</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator
ControlToValidate="rlist_type"
ErrorMessage="Card Type"
InitialValue=""
Text="*"
runat="server"/>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button id="b1" Text="Submit" runat="server"/></td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<asp:ValidationSummary
HeaderText="You must enter a value in the following fields:"
DisplayMode="BulletList"
EnableClientScript="true"
runat="server"/>
</form>
</body>
</html>
Please read carefully...
ReplyDelete