Skip to main content

ASP.NET Validation Server Controls

ASP.Net validation controls validate the user input data to ensure that useless, unauthenticated or contradictory data don’t get entered. ASP.NET validation controls provide an easy-to-use but powerful mechanism of ensuring that data is entered correctly on the forms. 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.

Client-Side and or Server-Side Validation: ASP.NET Validation Controls provides two types of validations:
·         Client-side validation: We can enable or disable the Client-side validation by setting a true/false value to the EnableClientScript property of the Validation Control. We don’t need to do anything special for Client-Side validation except setting the property EnableClientScript=true. By enabling Client-side validation we can reduce the round trips.
·         Server-side validation: If you want to do validation with your server side code (i.e. in C#, VB.Net language etc.), then server side validation is used.

If you want to forcefully disable client-side validation and perform only server-side validation for the page then set the page attribute ClientTarget to DownLevel as:
<%@ Page ClientTarget="DownLevel" %>

Force control to both side validation as:
<%@ Page ClientTarget="UpLevel" %>

Server Controls Property Related to Validation Controls
CausesValidation and ValidationGroup are the validation control related properties associated with some of the web server controls.

Property
Description
CausesValidation
Boolean value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus. If CausesValidation property is set to true, then validation is performed and if the CausesValidation property is set to false then validation is not done. By default CausesValidation is set to true.
This property is available to:
·         Button, ImageButton, and LinkButton Web server controls,
·         HtmlInputButton, HtmlInputImage, and HtmlButton HTML server controls
·         Controls that can automatically post back to the server such as the TextBox, CheckBox, ListControl, and BulletedList controls.
ValidationGroup
The group of controls for which the server control causes validation when it posts back to the server.
The name of the validation group to which this validation control belongs (Only controls having same validation group will be validated).
If no validation group is defined then all control are validated of page or control.

Page Property or Method Related to Server-Side Validation Controls
There are two properties and one method associated with page class related to validation controls, they are listed below.

Property
Description
IsValid
Gets a value indicating whether page validation succeeded? 
Validators
This property will return a collection of validation controls in the page
Method
Description
Validate()
Validate method is the main method that will be used for validating the validation controls from server side.

·         Validate(): Validating the All Validation Controls with in the page  
·         Validate(String): Validating Validation Controls for a specified validation group 

Client Side Property or Method Related to Validation Controls
ASP.NET validation controls provide functionality to perform validation using client script. When client-side validation is being performed, the user cannot post the page to the server if there are errors on the page thus reducing round-trips. The following properties are available in client-side. 

Property
Description
Page_IsValid
It is client side value used to identify the page is error free from the client side itself.  
Page_Validators
This property will return the collection of validation controls in the page.
Page_ValidationActive
Indicates whether validation should take place. Set this variable to False to turn off validation programmatically. 
Isvalid
Boolean value returns the associated validation control's validation failed or succeeds.
Method
Description
ValidatorValidate(val)
Takes a client-validator as input. Makes the validator check its input and update its display. 
ValidatorEnable(val, enable)
Enables or disables a client validator. Here "val" is the validation control and "enable" is the Boolean value. An example given in the section Conditional Validation.
ValidatorHookupControl
(control, val)
Takes an input HTML element and a client-validator. Modifies or creates the element's change event so that it updates the validator when changed. This can be useful for custom validators that depend on multiple input values.
Page_ClientValidate()
Client-side function to validate all the Validation Controls with in the page.
Page_ClientValidate(ValGroup)
Client-side function to validate all the Validation Controls with in the page  with the specified ValidationGroup

Validation Controls: There are 6 validation controls available in the ASP.NET.

2.    RangeValidator
5.    CustomValidator

Note: You can also create your own custom validator control, In order to create a CustomValidationControl you have to derive from the 'BaseValidator' class and implement the 'EvaluateIsValid()' method.

The BaseValidator Class: The validation control classes inherit from the BaseValidator class and inherit its properties and methods. Therefore, it would help to take a look at the properties and the methods of this base class, which are common for all the validation controls.

Common Properties of validation controls: Let’s look at the common properties of all the validation controls:

Property
Description
Note
BackColor
The background color of the validator control
Not available for ValidationSummary
ControlToValidate
The Id of the control to validate
Not available for ValidationSummary
Display
The display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
Not available for ValidationSummary
EnableClientScript
A Boolean value that specifies whether client-side validation is enabled or not

Enabled
A Boolean value that specifies whether the validation control is enabled or not

ErrorMessage
The text to display in the ValidationSummary control when validation fails.
Note: This text will also be displayed in the validation control if the Text property is not set
Not available for ValidationSummary
ForeColor
The foreground color of the control

Id
A unique Id for the validation control

IsValid
A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
Not available for ValidationSummary
runat
Specifies that the control is a server control. Must be set to "server"

Text
The message to display when validation fails
Not available for ValidationSummary

You can download sample code to see all the validation control in action.

Happy Programming!!!

Comments

Popular posts from this blog

Error 405 : ASP.NET Core Web API PUT and DELETE Methods not allowed

Recently, while working with .Net core API I came across the issue of “Error 405 — Methods not Allowed” After some research, I found out that both GET and POST requests working fine but neither PUT nor DELETE requests working. Another thing is that the PUT and DELETE request was also working fine on my local machine but failed when we host on our Azure server. When I explored the issue on the web it led me to the conclusion that WebDAVModule seems to set PUT and DELETE request methods disabled by default and due to that PUT and DELETE throw 405 errors. To make the PUT and DELETE requests work, we need to override the WebDAVModule setting in web.config file by adding the below settings under “ system.webServer ”. < system.webServer >   < modules runAllManagedModulesForAllRequests = " false " >     < remove name = " WebDAVModule " />   </ modules > </ system.webServer > There may be 2 web.config files in y...

C#: Merging Excel cells with NPOI HSSFWorkbook

In this post we’ll see how to merge the two or more cell with each other while creating the excel sheet using NPOI . Mentioned below is code to merge multiple cells, in this example we are merging first cell to fifth cell of first row (you can adjust row or cell range by passing particular parameters in CellRangeAddress). //Created new Workbook var hwb = new NPOI.HSSF.UserModel. HSSFWorkbook (); //Create worksheet with name. var sheet = hwb.CreateSheet( "new sheet" ); //Create row and cell. var row = sheet.CreateRow(0); var cell = row.CreateCell(0); ; //Set text inside cell cell.SetCellValue( "This is Merged cell" ); cell.CellStyle.WrapText = true ; //define cell range address // parameters: -> first row to last and first cell to last cell var cra = new NPOI.SS.Util. CellRangeAddress (0, 0, 0, 4); //Add merged region to sheet. sheet.AddMergedRegion(cra); Hope this solution helps you J

How to set Swagger as the default start page for API hosted on the Azure web app?

I created an Asp.Net Core 2.x Web API and configured Swagger on it, below is the code added in Configure method under Startup.cs file, for full swagger configuration, check here //Add swagger configuration app.UseSwagger(); app.UseSwaggerUI(c => {     c.SwaggerEndpoint( "../swagger/v1/swagger.json" , "Test API V1" ); }); On my local machine when I run the API it is automatically redirected to the Swagger page. However, when I hosted this API as an Azure web app it is not redirecting directly to the Swagger and to access the swagger, I had to append /swagger in the URL, for example, https://testapi.azurewebsites.net/swagger/ Solution: Set RoutePrefix to string.Empty under app.UseSwaggerUI like below: app.UseSwaggerUI(c => {     c.SwaggerEndpoint( "../swagger/v1/swagger.json" , "Test API V1" );      c.RoutePrefix = string .Empty; // Set Swagger UI at apps root }); And that’s it, now when you b...