# Add Validations

Learn the different data validation operations that can be implemented in DsExcel, such as adding a whole number validation, decimal validations, and more.

## Content

You can use the [Add method](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IValidation.Add.html) of the [IValidation interface](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IValidation.html) to apply data validation to individual cells or a range of cells in a spreadsheet. A single cell can have only one validation rule and if you try to apply validation on a cell that already possesses a validation rule, it will throw an exception.
Validation rule instance for a range is represented with the [Validation property](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Validation.html) of the [IRange interface](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.html). If you want to know whether a cell range already contains the validation rule, you can use the [HasValidation property](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.HasValidation.html) of the IRange interface. If all the cells in a range possess the same validation rule applied to them, it is represented with the [ValidationIsSame property](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.ValidationIsSame.html) of the IRange interface.
Shared below is a list of data validations operations that can be implemented in DsExcel .NET.

* [Add Whole Number Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-whole-number-validation)
* [Add Decimal Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-decimal-validation)
* [Add List Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-list-validation)
* [Add Date Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-date-validation)
* [Add Time Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-time-validation)
* [Add Text Length Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-text-length-validation)
* [Add Custom Validation](/document-solutions/dot-net-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-custom-validation)

## Add whole number validation

You can validate your data and ensure users add only whole numbers in cells or a range of cells by applying the whole number validation in a worksheet.
Refer to the following example code to add whole number validation.

```csharp
//Add whole number validation
worksheet.Range["A1:A3"].Validation.Add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 8);
IValidation validation = worksheet.Range["A1:A3"].Validation;
validation.IgnoreBlank = true;
validation.InputTitle = "Tips";
validation.InputMessage = "Input a value between 1 and 8, please";
validation.ErrorTitle = "Error";
validation.ErrorMessage = "input value does not between 1 and 8";
validation.ShowInputMessage = true;
validation.ShowError = true;
```

## Add decimal validation

You can validate your data and ensure users add only decimal numbers in cells or a range of cells by applying the decimal validation in a worksheet.
Refer to the following example code to add decimal validation.

```csharp
//Add Decimal validation
worksheet.Range["B1:B3"].Validation.Add(ValidationType.Decimal, ValidationAlertStyle.Stop, ValidationOperator.Between, 3.4, 102.8);
```

## Add list validation

You can also validate lists inserted in cells or a range of cells by applying the list validation in your worksheet .
Refer to the following example code to add list validation.

```csharp
//Add List Validation
worksheet.Range["C4"].Value = "aaa";
worksheet.Range["C5"].Value = "bbb";
worksheet.Range["C6"].Value = "ccc";
            
//Use cell reference.
worksheet.Range["C1:C3"].Validation.Add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "=c4:c6");

//Or use string.
//this._worksheet.Range["C2:E4"].Validation.Add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "aaa, bbb, ccc");

//Display list dropdown
IValidation dvalidation = worksheet.Range["C1:C3"].Validation;
dvalidation.InCellDropdown = true;
```

>type=note
> **Note**: When validating a cell containing a picture:
>
> * For comma-separated text lists, validation succeeds if the picture's alt text exactly matches one of the list items.
> * For range reference lists, validation succeeds only if both the alt text and richdata are identical between the picture cell and the source cell.
> * A cell containing a picture cannot be validated against a text-only source cell, and a text cell cannot be validated against a picture-only source cell.

## Add date validation

You can validate data entered in date format in cells or a range of cells by applying the date validation in a worksheet.
Refer to the following example code to add date validation.

```csharp
//Add Date validation
worksheet.Range["D1:D3"].Validation.Add(ValidationType.Date, ValidationAlertStyle.Stop, ValidationOperator.Between, new DateTime(2015, 12, 13), new DateTime(2015, 12, 18));
```

## Add time validation

You can validate the time entered in cells or a range of cells by applying the time validation in a worksheet.
Refer to the following example code to add time validation.

```csharp
//Add Time Validation
worksheet.Range["E1:E3"].Validation.Add(ValidationType.Time, ValidationAlertStyle.Stop, ValidationOperator.Between, new TimeSpan(13, 30, 0), new TimeSpan(18, 30, 0));
```

## Add text length validation

You can validate the length of the text entered in cells or a range of cells by applying the text length validation in a worksheet.
Refer to the following example code to add text length validation.

```csharp
//Add Text Length Validation
worksheet.Range["C2:E4"].Validation.Add(ValidationType.TextLength, ValidationAlertStyle.Stop, ValidationOperator.Between, 2, 3);
```

## Add custom validation

You can add a custom validation rule to validate data in a worksheet by applying custom validation.
Refer to the following example code to add custom validation.

```csharp
//Add custom validation
worksheet.Range["A2"].Value = 1;
worksheet.Range["A3"].Value = 2;
worksheet.Range["C2"].Value = 1;
//when use custom validation, validationOperator and formula2 parameters will be ignored even if you have given.
worksheet.Range["A2:A3"].Validation.Add(ValidationType.Custom, ValidationAlertStyle.Information, formula1: "=C2");
```

>type=note
> **Note**: When validating a cell containing a picture in cell using custom validation, the validation result is determined solely by the formula's return value:
>
> * If the formula returns FALSE, the validation is invalid.
> * If the formula returns any other value (e.g., TRUE, number, text), the validation is valid.