ASP.NET Validators

Discussion in 'ASP.NET' started by MinalS, Jul 27, 2015.

  1. There are various validation controls provided by ASP.NET. They are added to the controls for validating the input values added by the user.

    BaseValidator class



    The validator consists of the properties inherited from the BaseValidator class. The properties and methods used by the validation controls are as listed below.
    1. Display: The error message used for the specific value is shown
    2. ControlToValidate: The control to be validated is assigned by the user
    3. ErrorMessage: The error value shown when the validation is performed
    4. EnableClientScript: The client side validation is initiated
    5. Validate(): The control is validated and the IsValid property is validated

    Range Validator



    The range validator is used for checking the values defined in a specific range.

    Properties of the range validator are:
    1. BackColor: The background color of the web server control is assigned.
    2. BindingContainer: The data binding of the control is defined
    3. Context: The HttpContext object associated with the server control is defined
    4. Display: The error message to be shown when the validation control is stated
    5. EnableTheming: The value used for showing the themes applied to the control
    6. ID: The identifier assigned to the server control
    7. MinimumValue: The minimum value of the validation is assigned
    8. MaximumValue: The maximum value of the validation is assigned
    9. Style: The text attributes which are rendered as the style attributes are defined
    Some of the methods of Range Validator.
    1. AddedControl: Once the child control is added to the collection, it is called by the user
    2. ApplyStyle: The non empty elements are applied a specific style to the control
    3. ClearChildState: The view state and control state information for the controls is removed
    4. DataBind: Used for binding the data source to the server control and child controls
    5. Finalize: The resources are removed from the system and are garbage collected
    6. HasControls: If there are child controls present in the server controls
    7. OnLoad: The Load event is raised by the user
    Syntax of range validator:
    Code:
    <asp:RangeValidator ID="rg1" runat="server" ControlToValidate="TextBox1" MinumumValue="10" MaximumValue="50" ErrorMessage="Values not in range" Type="Integer" >
    </asp:RangeValidator>
    

    RequiredField Validator



    The required field validator is used to check that the field contains value.

    Some of the properties of the required field validator are:
    1. Attributes: The collection of the attributes not corresponding to the properties is shown.
    2. ClientID: The control ID for the HTML markup created by ASP.NET
    3. ControlToValidate: The input control to be validated by the user
    4. Display: The behavior of the error message in the validation control is shown.
    5. ErrorMessage: The error message for the control when it fails validation
    6. ID: The unique identifier for the control is defined
    7. Width: The width of the control can be assigned
    Some of the methods of the required field validator are
    1. AddedControl: It is called when the child control is added to the collection
    2. ApplyStyle: The non empty elements stated by the style of the control are copied.
    3. ClearChildControlState: The control state information is deleted from the server child controls
    4. Focus: Provides the focus to the control
    5. HasEvents: The value stating the events registered for the controls and the corresponding child controls
    6. SaveViewState: The state is saved which is modified after the method was invoked
    7. ToString: The string stating the current object is returned
    8. Validate: The validation with the corresponding control is performed
    Syntax of RequiredField Validator:

    Code:
    <asp:RequiredFieldValidator ID="field1" runat="server" ControlToValidate="textbox1" ErrorMessage="Value must be added"></asp:RequiredFieldValidator>
    

    Regular expression validator



    The validator is used for validating the value present in the control is similar to the expression defined.

    Some of the properties of RegularExpressionValidator are:
    1. AccessKey: Provides the access key used for navigating the server control
    2. Context: The HttpContext object associated with the control is retrieved
    3. CssClass: The CSS class is rendered through the web server control
    4. Display: The display of the error message in the validation control is defined
    5. Events: The list of event handler used as delegates for the control
    6. ID: The identifier associated with the specific server control is defined
    7. SkinID: The skin to be added to the control is defined
    8. TemplateControl: The reference of the template is defined
    Some of the methods of the RegularExpressionValidator are:
    1. BuildProfileTree: The information about the server control is collected and provided as an input to the Trace property
    2. ClearChildControlState: User for removing all the information of view and control state for all the server controls
    3. EndRenderTracing: The design time rendering of the data is ended
    4. Finalize: The object is allowed to free the resources and the cleanup operations are performed
    5. GetType: The Type of the current instance is provided
    6. OnBubbleEvent: It checks if the server control is passed to the page UI server control structure
    7. ResolveUrl: The URL is converted to the usable one by the client
    8. SetDesignModeState: The design time data for the control is set
    Syntax for RegularExpression validator is:
    Code:
    <asp:RegularExpressionValidator ID="reg1" runat="server" ControlToValidate="Control1" ErrorMessage="Show the string" ValidationExpression="string" >
    </asp:RegularExpression>
    

    Compare validator



    It is used for comparing the values added by the user in the input control or with a constant value defined by the user.

    Properties of Compare validator:
    1. BindingContainer: It provides the control consisting of the data binding
    2. BorderWidth: The border of the control can be modified
    3. ControlStyle: The style of the web server control is defined
    4. Enabled: The value stating the validation control is enabled or not
    5. Events: The list of event handler delegate to the control are provided
    6. Operator: The operator used for the comparison is defined
    7. TemplateControl: The reference of the template of the control is provided
    8. ValueToCompare: It accesses or assigns the value which is compared by the user value
    Methods of Compare Validator:
    1. AddAtttributesToRender: The attributes of the control are added to the output stream.
    2. BeginRenderTracing: The design time rendering of the data is performed
    3. EndRenderTracing: The design time rendering is stopped by the user
    4. GetControlRenderID: The clientID for the control is retrieved
    5. IsLiteralContent: It checks whether the server control contains literal content
    6. OpenFile: The stream used for reading the file
    Syntax for Compare Validator:

    Code:
    <asp:CompareValidator ID="Compare1" runat="server" ControlToValidate="control1" ControlToCompare="control2" ErrorMessage="Values must match" Operator="Equals">
    </asp:CompareValidator>
    

    Custom validator



    The user defined validations are performed on the user control. The client and server side validation are used. The ClientValidationFunction property is used to perform the client side validation. The ServerValidate event is used for validating the server side.

    Properties of custom validator:
    1. Adapter: The browser related adapter for the control is defined
    2. ControlStyle: The style for the web server control is defined
    3. ControlToValidate: The control validated for the user inputs
    4. DesignMode: The value stating the control used in the design surface
    5. IdSeperator: The character used to change the identifiers
    6. Parent: The reference to the server control is defined
    Methods of custom validator:
    1. AddParsedSubObject: The notification for every element parsed and added
    2. ClearChildState: The view state and control state for the servers child controls is defined
    3. ControlPropertiesValid: It checks whether the control contains valid values
    4. DetermineRenderUplevel: It decides whether the validation control can perform client side validation
    5. GetHashCode: The hash function for the control is retrieved
    6. RemovedControl: Once the child control is removed, it is called by the user
    7. SaveControlState: The server control state changed are modified by the user
    Syntax for custom validator control is:

    Code:
    <asp:CustomValidator ID="custom1" runat="server" ClientValidationFunction="function1" ErrorMessage="CustomValidator values" >
    </asp:CustomValidator>
    

    Validation summary



    The Validation summary control is used for displaying the error messages present on the web page. The ErrorMessage property is used for showing the values when the validation is not performed.

    Syntax for Validation summary:

    Code:
    <asp:ValidationSummary ID="validation1" runat="server" DisplayMode="BulletList" ShowSummary="false" >
    </asp:ValidationSummary>
    
    Example to demonstrate the validation controls in an ASP.NET application
    1. Open Visual Studio application in your system. Create a web application
    2. In the source view, add the following code.
      Code:
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      	<title></title>
      </head>
      
      <body>
      	<form id="form1" runat="server">
      	<div>
      	<table style="width:77%";>
      	<tr>
      		<td colspan="3" align="center">
      		<asp:Label ID="lbl1" runat="server" Text="Student admission form" >
      		</asp:Label>
      		</td>
      	</tr>
      		
      	<tr>
      		<td>
      		StudName:
      		</td>
      		
      		<td>
      		<asp:TextBox ID="Text1" runat="server"></asp:TextBox>
      		</td>
      	
      		<td>
      		<asp:RequiredFieldValidator ID="req1" runat="server" ControlToValidate="Text1" ErrorMessage="Value must be added" >
      		</asp:RequiredFieldValidator>
      		</td>
      	</tr>
      
      	<tr>
      		<td>
      		StudAge:
      		</td>
      	
      		<td>
      		<asp:TextBox ID="text2" runat="server"></asp:TextBox>
      		</td>
      	
      		<td>
      		<asp:RangeValidator ID="range1" runat="server" MaximumValue="50" MinimumValue="20" ControlToValidate="text2" ErrorMessage="Values not in range" >
      		</asp:RangeValidator>
      		</td>
      		
      	</tr>
      
      	<tr>
      		<td>
      		Email:
      		</td>
      		
      		<td>
      		<asp:TextBox ID="text3" runat="server"></asp:TextBox>
      		</td>
      	
      		<td>
      		<asp:RegularExpressionValidator="reg1" runat="server" ControlToValidate="text3" ErrorMessage="Email not in proper format" ValidationExpression="\w+([-+.’)\w+)*@\w+([-.]\w+)*\.\w+([-.])\w+)*" >
      		</asp:RegularExpressionValidator>
      		</td>
      	</tr>
      
      	<tr>
      		<td>
      		<asp:Button ID="btn1" runat="server" Text="Show" />
      		</td>
      	</tr>
      </table>
      
      <asp:ValidationSummary ID="valid1" runat="server" DisplayMode="BulletList" ShowSummary="true" />
      
      </div>
      </form>
      </body>
      
    3. When the code is compiled and executed, the following result is displayed.

      [​IMG]
     
    Last edited by a moderator: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice