|
Validation can be applied on both client-side and server-side. The widely used client-side validation is javascript. Asp.Net validation controls provide two types of validation i.e Server and client side. These validation controls will perform client-side validation when they detect the browser is able (unless client-side validation has been disabled). This will reduce extra roundtrip to the server. And this will perform server side where ever necessary.
There are 6 validation controls.
- RequiredFieldValidator Control
- CompareValidator Control
- RangeValidator Control
- RegularExpressionValidator Control
- CustomValidator Control
- ValidationSummary
BaseValidator Class: All validator controls are inherited from BaseValidator class so they have enjoyed some common methods and properties.
- ControlToValidate - This attribute contains the ID of the control that need to be validate.
- ErrorMessage - Error message that will be displayed in the validation summary.
- IsValid -This provide the check whether the control is valid or not.
- Validate - Method to validate the input control and update the IsValid property.
- Display - This attribute is responsible to display/shown the error message. The options are none, static, dynamic.
RequiredFieldValidation Control
This control will check whether the user input any value in the control that we are validating or not.
Eg.
<asp: textbox id=”txtUser” runat=”server”/>
<asp: RequiredFieldValidator id=”valReqUser” runat=”server” ControlToValidate=”txtUser” ErrorMessage=”UserName is Required” Display=”dynamic”>
</asp:RequiredFieldValidator>
CompareValidator Control
This control is used to compare the data of two controls.
Eg.
<asp: textbox id=”txtPassword” runat=”server”/>
<asp: textbox id=”txtConfirmPassword” runat=”server”/>
<asp: CompareValidator id=”valCompare” runat=”server” ControlToValidator=”txtPassword” ControlToCompare=”txtConfirmPassword” Operator=”Equals” ErrorMessage=”Please insert same values.” Display=”dynamic”>
</asp: CompareValidator>
Here the Operator contains options like equal, greaterthanequal, less than etc. We can also compare the control value with the numeric, string, currency, double and date values using Type attribute of the CompareValidator.
RangeValidator Control:
This control checks whether the control value which we are going to validate is within the valid range that is specified or the user input value is between specified lower and upper boundaries.
<asp: textbox id=”txtAge” runat=”server”/>
<asp: RangeValidator id=”rngVal” ControlToValidate=”txtAge” MinimumValue=”20” MaximumValue=”30” Type=”Integer” Error Message=”Age can't be less than 20 and greater than 30” Text=”Age doesn't met the criteria” runat=”server” />
The ControlToValidate attribute specify the control name that we need to validate, the maximum and minimum value define the boundaries for the value that is inserted in the control and Type define the data type , type can be Integer, String, Double, Date , Currency.
RegularExpressionValidator Control
This control is used for matching user input against a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as e-mail address, social security numbers, telephone numbers, and postal codes and so on.
<asp: textbox id=” txtzipcode ” runat=”server”/>
<asp: RegularExpressionValidator id=”rgExpVal” ControlToValidate = “txtzipCode” ValidationExpression = “\d{5}” Error Message=”The zip code must be 5 numeric digits!.” Text=” The zip code must be 5 numeric digits!.” Runat=”server” />
Use the ValidationExpression property to specify the regular expression used to validate input control. The regular expression validation syntax is slightly different on the client than on the server.On the Client, Jscript regular expression syntax is used. On the server Regex syntax is used. Note: Jscript is subset of Regex syntax, so use Jscript regular expression in order for same results on both client and server side.
CustomValidator Control:
The control is used for defining custom server and client validation code.
<script runat=”server”>
Sub ServerValidateFunction(s as Object,arg as ServerValidateEventArgs)
If len (arg.Value) < 8 or len (arg.Value) > 16 Then
arg.IsValid = False
Else
arg.IsValid = True
End If
</script>
<asp: Label runat="server" Text="Enter a username: " />
<asp: Textbox id="txtUserName" runat="server" />
<asp: Button Text="Submit" runat="server"/>
<asp: CustomValidator id=”val1” ControlToValidate=”txtUserName” ClientValidationFunction=”” OnServerValidate = “ServerValidateFunction” ErrorMessage=” A username must be between 8 and 16 characters” Text=” A username must be between 8 and 16 characters” Runat=”server” />
This control allow you to create a validation control with its validation handler. To create a server-side validation function, Provide a handler for the ServerValidate event that performs the Validation.The string from the input control to validate can be accessed by using the Value property of the ServerValidateEventArgs object passed into the event handler as a parameter.The result is then stored in the Is Valid property of the ServerValidateEventArgs object.
Note:- To create a client-side validation function, first add the server-side validation function described earlier. Next, add the client-side validation script function to the .aspx page.
Validation Summary
Display a list of all validation errors on the Web Page. This will help in summarize the entire error message at one location.
<asp:ValidationSummary id=”ID” DisplayMode = “BulletList | List | SingleParagraph” EnableClientScript = true| False ShowSummary = true | False ShowMessageBox = true | False HeaderText =”Text To display as summary Title” Runat=”server” /> |