A Validation server control is used to validate the data of an input control. If the data does not pass validation, it will display an error message to the user.
CompareValidator : Compares the value of one input control to the value of another input control or to a fixed value [ Read.. ]
CustomValidator: Allows you to write a method to handle the validation of the value entered [ Read.. ]
RangeValidator: Checks that the user enters a value that falls between two values [ Read.. ]
RegularExpressionValidator: Ensures that the value of an input control matches a specified pattern [ Read.. ]
RequiredFieldValidator: Makes an input control a required field [ Read.. ]
CompareValidator : Compares the value of one input control to the value of another input control or to a fixed value [ Read.. ]
CustomValidator: Allows you to write a method to handle the validation of the value entered [ Read.. ]
RangeValidator: Checks that the user enters a value that falls between two values [ Read.. ]
RegularExpressionValidator: Ensures that the value of an input control matches a specified pattern [ Read.. ]
RequiredFieldValidator: Makes an input control a required field [ Read.. ]
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>
The ASP.NET Framework contains over 70 controls.
You should always assign an ID attribute to every control even when you don’t need to program against it.
. Standard Controls—The standard controls enable you to render standard form elements such as buttons, input fields, and labels.
. Validation Controls—The validation controls enable you to validate form data before you submit the data to the server. For example, you can use a RequiredFieldValidator control to check whether a user entered a value for a required input field.
. Rich Controls—The rich controls enable you to render things such as calendars, file upload buttons, rotating banner advertisements, and multi-step wizards.
. Data Controls—The data controls enable you to work with data such as database data. For example, you can use these controls to submit new records to a database table or display a list of database records.
. Navigation Controls—The navigation controls enable you to display standard navigation elements such as menus, tree views, and bread crumb trails.
. Login Controls—The login controls enable you to display login, change password, and registration forms.
. HTML Controls—The HTML controls enable you to convert any HTML tag into a server- side control.
You should always assign an ID attribute to every control even when you don’t need to program against it.
. Standard Controls—The standard controls enable you to render standard form elements such as buttons, input fields, and labels.
. Validation Controls—The validation controls enable you to validate form data before you submit the data to the server. For example, you can use a RequiredFieldValidator control to check whether a user entered a value for a required input field.
. Rich Controls—The rich controls enable you to render things such as calendars, file upload buttons, rotating banner advertisements, and multi-step wizards.
. Data Controls—The data controls enable you to work with data such as database data. For example, you can use these controls to submit new records to a database table or display a list of database records.
. Navigation Controls—The navigation controls enable you to display standard navigation elements such as menus, tree views, and bread crumb trails.
. Login Controls—The login controls enable you to display login, change password, and registration forms.
. HTML Controls—The HTML controls enable you to convert any HTML tag into a server- side control.