1. RequiredFieldValidator
The RequiredFieldValidator control ensures that the user does not skip an entry. The control fails validation if the value it contains does not change from its initial value when validation is performed. If all the fields in the page are valid, the page is valid
The RequiredFieldValidator control ensures that the user does not skip an entry. The control fails validation if the value it contains does not change from its initial value when validation is performed. If all the fields in the page are valid, the page is valid
<form runat=server> Name: <asp:TextBox id=Text1 runat="server"/> <asp:RequiredFieldValidator id="RequiredFieldValidator1" ControlToValidate="Text1" Font-Names="Arial" Font-Size="11" runat="server"> Required field! </asp:RequiredFieldValidator> <p> <asp:Button id="Button1" runat="server" Text="Validate" /> </form>
It will display message when any field or control is left blank
We can validate any control using ControlToValidate property.
2.RangeValidator
The RangeValidator control tests whether an input value falls within a given range. RangeValidator uses three key properties to perform its validation: ControlToValidatecontains the value to validate, MinimumValue defines the minimum value of the valid range, and MaximumValue defines the maximum value of the valid range. These constants are stored as string values, but are converted to the data type defined by Type when the comparison is performed
<form runat="server">
<table bgcolor="#eeeeee" cellpadding=10>
<tr valign="top">
<td>
<h5><font face="Verdana">Value to Check:</font></h5>
<asp:TextBox Selected id="txtComp1" runat="server"/>
</td>
<td>
<h5><font face="Verdana">Data Type: Integer Min(1), Max(10)</font></h5>
</td>
<td>
<asp:Label id="lblOutput1" Font-Names="verdana" Font-Size="10pt" runat="server" />
</td>
</tr>
<tr valign="top">
<td>
<h5><font face="Verdana">Value to Check:</font></h5>
<asp:TextBox Selected id="txtComp2" runat="server"/>
</td>
<td>
<h5><font face="Verdana">Data Type: Date Min(2000/1/1), Max(2001/1/1)</font></h5>
</td>
<td>
<asp:Label id="lblOutput2" Font-Names="verdana" Font-Size="10pt" runat="server" />
</td>
</tr>
<tr valign="top">
<td>
<h5><font face="Verdana">Value to Check:</font></h5>
<asp:TextBox Selected id="txtComp3" runat="server"/>
</td>
<td>
<h5><font face="Verdana">Data Type: String Min(Aardvark), Max(Zebra)</font></h5>
</td>
<td>
<asp:Label id="lblOutput3" Font-Names="verdana" Font-Size="10pt" runat="server" />
</td>
</tr>
</table>
<asp:Button Text="Validate" ID="Button1" onclick="Button1_Click" runat="server" />
<asp:RangeValidator
id="rangeValInteger"
Type="Integer"
ControlToValidate="txtComp1"
MaximumValue="10"
MinimumValue="1"
runat="server" ErrorMessage="Please Enter Value between 1 to 10"/>
<asp:RangeValidator
id="rangeValDate"
Type="Date"
ControlToValidate="txtComp2"
MaximumValue="2001/1/1"
MinimumValue="2000/1/1"
runat="server" ErrorMessage="Please enter valid date"/>
<asp:RangeValidator
id="rangeValString"
Type="String"
ControlToValidate="txtComp3"
MaximumValue="Zebra"
MinimumValue="Aardvark"
runat="server" ErrorMessage="Please enter valid data"/>
<br>
</form>
ErrorMessage is shown when any data crosses the given range
3.RegularExpressionValidator
The RegularExpressionValidator control confirms that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.
RegularExpressionValidator uses two key properties to perform its validation: ControlToValidate contains the value to validate, and ValidationExpression contains the regular expression to match.
We can validate the content using regularexpressionvalidator
such as TextBox must contain only string or int etc..
<table bgcolor="#eeeeee" cellpadding=10>
<tr valign="top">
<td colspan=3>
<asp:Label ID="lblOutput" Text="Enter a 5 digit zip code" Font-Names="Verdana" Font-Size="10pt" runat="server"/>
</td>
</tr>
<tr>
<td colspan=3>
<font face=Verdana size=2><b>Personal Information</b></font>
</td>
</tr>
<tr>
<td align=right>
<font face=Verdana size=2>Zip Code:</font>
</td>
<td>
<ASP:TextBox id=TextBox1 runat=server />
</td>
<td>
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1"
ValidationExpression="\d{5}"
Display="Static"
Font-Names="verdana"
Font-Size="10pt">
Zip code must be 5 numeric digits
</asp:RegularExpressionValidator>
</td>
</tr>
</table>
For String use: [a-zA-Z\s]+
For Integer use:[0-9\s]+
3. CompareValidator
uses three key properties to perform its validation. ControlToValidate and ControlToCompare contain the values to compare. Operator defines the type of comparison to perform, for example, Equal or Not Equal. CompareValidator performs the validation by evaluating these properties as an expression, as shown in the following example.
<ControlToValidate> <Operator> <ControlToCompare>
<table bgcolor="#eeeeee" cellpadding=10>
<tr valign="top">
<td>
<h5><font face="Verdana">String 1:</font></h5>
<asp:TextBox Selected="True" id="txtComp" runat="server"></asp:TextBox>
</td>
<td>
<h5><font face="Verdana">Comparison Operator:</font></h5>
<asp:ListBox id="lstOperator" OnSelectedIndexChanged="lstOperator_SelectedIndexChanged" runat="server">
<asp:ListItem Selected="True" Value="Equal" >Equal</asp:ListItem>
<asp:ListItem Value="NotEqual" >NotEqual</asp:ListItem>
<asp:ListItem Value="GreaterThan" >GreaterThan</asp:ListItem>
<asp:ListItem Value="GreaterThanEqual" >GreaterThanEqual</asp:ListItem>
<asp:ListItem Value="LessThan" >LessThan</asp:ListItem>
<asp:ListItem Value="LessThanEqual" >LessThanEqual</asp:ListItem>
</asp:ListBox>
</td>
<td>
<h5><font face="Verdana">String 2:</font></h5>
<asp:TextBox id="txtCompTo" runat="server"></asp:TextBox><p>
<asp:Button runat=server Text="Validate" ID="Button1" onclick="Button1_OnSubmit" />
</td>
</tr>
</table>
<asp:CompareValidator id="comp1" ControlToValidate="txtComp" ControlToCompare = "txtCompTo" Type="String" runat="server"/>
4. CustomValidator
The CustomValidator control calls a user-defined function to perform validations that the standard validators can't handle. The custom function can execute on the server or in client-side script, such as JScript or VBScript. For client-side custom validation, the name of the custom function must be identified in the ClientValidationFunctionproperty. The custom function must have the form function myvalidator(source, arguments)
Note that source is the client-side CustomValidator object, andarguments is an object with two properties, Value and IsValid. The Value property is the value to be validated and the IsValid property is a Boolean used to set the return result of the validation. You can view a client-side validation example in the Validating Form Input Controls section.
For server-side custom validation, place your custom validation in the validator's OnServerValidate delegate.
<html>
<head>
<script language="VB" runat=server>
Sub ValidateBtn_OnClick(sender As Object, e As EventArgs)
If (Page.IsValid) Then
lblOutput.Text = "Page is valid!"
Else
lblOutput.Text = "Page is not valid! :-("
End If
End Sub
Sub ServerValidate (sender As Object, value As ServerValidateEventArgs)
Dim num As Integer
' even number?
If Integer.TryParse(value.Value, num) Then
value.IsValid = (num Mod 2 = 0)
Else
value.IsValid = false
End If
End Sub
</script>
</head>
<body>
<h3><font face="Verdana">CustomValidator Example</font></h3>
<form runat="server">
<asp:Label id=lblOutput runat="server"
Text="Enter an even number:"
Font-Names="Verdana"
Font-Size="10pt" /><br>
<p>
<asp:TextBox id=Text1 runat="server" />
  
<asp:CustomValidator id="CustomValidator1" runat="server"
ControlToValidate="Text1"
OnServerValidate="ServerValidate"
Display="Static"
Font-Names="verdana" Font-Size="10pt">
Not an even number!
</asp:CustomValidator>
<p>
<asp:Button text="Validate" onclick="ValidateBtn_OnClick" runat="server" />
</form>
</body>
</html>
No comments:
Post a Comment