Validation server controls are set of controls that enable you to work with the information your end users inputs into application.
ASP.NET 2.0 Validation server controls:
RequiredFieldValidator:
<asp:RequiredFieldValidator>
The validated control contains a value. It cannot be empty. As it's obvious, it make sure that a user inputs a value. we can use it to check if the input control has any value. The most important property in the RequiredFieldValidator is InitialValue.
<asp:textbox id="txtName" runat="server"/>
<asp:RequiredFieldValidator id="RegTxtName runat="server" controlToValidate="txtName" errorMessage="Name must be entered" Display="dynamic"> </asp:RequiredFieldValidator>
In this example, we have a textbox which will not be valid until the user types something in. Inside the validator tag. The text in the innerhtml will be shown in the controltovalidate if the control is not valid.
RegularExpressionValidator:
The regular expression validator is one of the more powerful features of ASP.NET. Everyone loves regular expressions. Especially when you write those really big nasty ones...
<asp:RegularExpressionValidator>
The value against a regular expression with pattern. Checks that the value in the control matches a specified regular expression. If the validated control is empty, no validation takes place. The most important property in the RegularExpressionValidator is ValidationExpression.
<asp:textbox id="txtEmail" runat="server"/>
<asp:RegularExpressionValidator id="regEmail" runat="server" display="static" controlToValidate="txtEmail" errorMessage="Your entry is not a valid e-mail address." validationExpression=".*@.*\..*"> </asp:RegularExpressionValidator>
CompareValidator:
This control is the ControlToCompare attribute which is the control that will be compared. The two controls are compared with the type of comparison specified in the Operator attribute. The Operator attribute can contain Equal, GreterThan, LessThanOrEqual, etc.
<asp:CompareValidator>
The value is acceptable compared to a given value or compared to the content of another control. In other words, it checks that the value in the validated control matches the value in another control or a specific value. The data type and comparison operation can be specified. If the validated control is empty, no validation takes place. The most important properties in the CompareValidator are ValueToCompare, ControlToCompare, Operator, and type.
<asp:textbox id="texValue" runat="server"/>
<asp:CompareValidator id="valRequired" runat="server" ControlToValidate="texValue" ValueToCompare="100" Type="Integer" Operator="GreaterThan" ErrorMessage="* You must enter the a digit greater than 100" Display="dynamic"> </asp:CompareValidator>
RangeValidator:
The RangeValidator Server Control makes sure that the end user value or selection provided is between a specified ranges.
<asp:RangeValidator>
If the input control�s value is within a specified range. In other words, it checks that the value in the validated control is within the specified text or numeric range. If the validated control is empty, no validation takes place. The most important properties in the RangeValidator are MaximumValue, MinimumValue, and type.
<asp:textbox id="texTotalBoys" runat="server"/>
<asp:RangeValidator id="RegCountBoys" runat="server" display="static" controlToValidate="texTotalBoys" errorMessage="Must be from 0 to 100" type="Integer" minimumValue=0 maximumValue=100> </asp:RangeValidator>
CustomValidator:
<asp:CustomValidator>
Allows you to develop custom validation. Performs user-defined validation on an input control using a specified function (client-side, server-side, or both). If the validated control is empty, no validation takes place. The most important property in the CustomValidator is ClientValidationFunction.
<asp:TextBox ID="textValue" runat="server"></asp:TextBox>
<asp:Button ID="BtnTest" runat="server" Text="Divisible" />
<asp:CustomValidator ID="RegCustomValidator" runat="server" ControlToValidate="textValue" ClientValidationFunction="validatenumber" ErrorMessage="Number is not divisible by 6"></asp:CustomValidator>
<script type="text/javascript">
function validatenumber(oSrc,args)
{
args.IsValid = (args.Value % 6 == 0)
}
</script>
ASP.NET 2.0 Validation server controls:
RequiredFieldValidator:
<asp:RequiredFieldValidator>
The validated control contains a value. It cannot be empty. As it's obvious, it make sure that a user inputs a value. we can use it to check if the input control has any value. The most important property in the RequiredFieldValidator is InitialValue.
<asp:textbox id="txtName" runat="server"/>
<asp:RequiredFieldValidator id="RegTxtName runat="server" controlToValidate="txtName" errorMessage="Name must be entered" Display="dynamic"> </asp:RequiredFieldValidator>
In this example, we have a textbox which will not be valid until the user types something in. Inside the validator tag. The text in the innerhtml will be shown in the controltovalidate if the control is not valid.
RegularExpressionValidator:
The regular expression validator is one of the more powerful features of ASP.NET. Everyone loves regular expressions. Especially when you write those really big nasty ones...
<asp:RegularExpressionValidator>
The value against a regular expression with pattern. Checks that the value in the control matches a specified regular expression. If the validated control is empty, no validation takes place. The most important property in the RegularExpressionValidator is ValidationExpression.
<asp:textbox id="txtEmail" runat="server"/>
<asp:RegularExpressionValidator id="regEmail" runat="server" display="static" controlToValidate="txtEmail" errorMessage="Your entry is not a valid e-mail address." validationExpression=".*@.*\..*"> </asp:RegularExpressionValidator>
CompareValidator:
This control is the ControlToCompare attribute which is the control that will be compared. The two controls are compared with the type of comparison specified in the Operator attribute. The Operator attribute can contain Equal, GreterThan, LessThanOrEqual, etc.
<asp:CompareValidator>
The value is acceptable compared to a given value or compared to the content of another control. In other words, it checks that the value in the validated control matches the value in another control or a specific value. The data type and comparison operation can be specified. If the validated control is empty, no validation takes place. The most important properties in the CompareValidator are ValueToCompare, ControlToCompare, Operator, and type.
<asp:textbox id="texValue" runat="server"/>
<asp:CompareValidator id="valRequired" runat="server" ControlToValidate="texValue" ValueToCompare="100" Type="Integer" Operator="GreaterThan" ErrorMessage="* You must enter the a digit greater than 100" Display="dynamic"> </asp:CompareValidator>
RangeValidator:
The RangeValidator Server Control makes sure that the end user value or selection provided is between a specified ranges.
<asp:RangeValidator>
If the input control�s value is within a specified range. In other words, it checks that the value in the validated control is within the specified text or numeric range. If the validated control is empty, no validation takes place. The most important properties in the RangeValidator are MaximumValue, MinimumValue, and type.
<asp:textbox id="texTotalBoys" runat="server"/>
<asp:RangeValidator id="RegCountBoys" runat="server" display="static" controlToValidate="texTotalBoys" errorMessage="Must be from 0 to 100" type="Integer" minimumValue=0 maximumValue=100> </asp:RangeValidator>
CustomValidator:
<asp:CustomValidator>
Allows you to develop custom validation. Performs user-defined validation on an input control using a specified function (client-side, server-side, or both). If the validated control is empty, no validation takes place. The most important property in the CustomValidator is ClientValidationFunction.
<asp:TextBox ID="textValue" runat="server"></asp:TextBox>
<asp:Button ID="BtnTest" runat="server" Text="Divisible" />
<asp:CustomValidator ID="RegCustomValidator" runat="server" ControlToValidate="textValue" ClientValidationFunction="validatenumber" ErrorMessage="Number is not divisible by 6"></asp:CustomValidator>
<script type="text/javascript">
function validatenumber(oSrc,args)
{
args.IsValid = (args.Value % 6 == 0)
}
</script>