Validator Control In ASP.NET

How to implement Validator Control in WebForm in .NET 4.5 version

By using validator control there is no need to write any single line of code in code behind file.

If you use validator control directly, you can face this yellow screen.

error

How to use validator control in web form with some settings

This article is the answer.

  1. Create a new website project.

    Click File, New Web Site, then ASP.NET Empty Web Site.

    Asp.Net Empty Web Site

  2. Right click on project.

    Add, Add New Item, then Web Form and name it default.aspx,

    web form

  3. Take the followings controls in your Default.aspx page.


  4. Web.Config file settings for using validator control in webform
    1. <appSettings>  
    2.    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>  
    3. </appSettings>  
  5. Validator Control properties details:

    You can see the following common properties in all validator control.

    RequiredFieldValidator properties used

    ID ID for each controls
    ErrorMessage Message which display if left blank or not fullfill our condition of control
    (This property used in all controls)
    ControlToValidate Specify which textbox or control you want to bind with this.
    ForeColor Type of color on error.
    Display Dynamic , Static.
    Dynamic: To Display message as per space availability
    Static: Fix place to display message.
    ValidationGroup This important property bind all the validator control and button control.

    CompareValidator additional properties used than RequiredFIeldValidator

    ControlToCompare: Specify the control name which you want to compare.

    RangeValidator additional properties used than RequiredFIeldValidator

    MinimumValue: Specify the Minimum value of control to accept from user.
    MaximumValue: Specify the Maximum value of control to accept from user.

    RegularExpressionValidator additional properties used than RequiredFIeldValidator

    ValidationExpression:

    There are so many readymade expression already in Visual studio. You can use those or create your own according to your requirement.

    ValidationExpression

  6. Normal screen when page load.

    page load

  7. Blank Form Submission: This time you can see all control display the required messages.

    run

  8. Now specific control error display like the following,

    1. Confirm Password not match with Password.
    2. Age not within 21 to 99
    3. Email ID not perfectly written.

      Login

Up Next
    Ebook Download
    View all
    Learn
    View all