# 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 apply data validation to restrict and verify the data entered in a single cell or a range of cells in a worksheet.
Only one validation rule should be applied for a cell. One cell cannot have multiple validation rules applied to it.
In case you try to validate a cell that already has a validation rule, an exception will be thrown.
If you want to know whether a cell range already contains the validation rule, you can use the methods of the [IRange](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html) interface. If all the cells in a range possess the same validation rule applied to them, you can check it using the methods of the IRange interface.
Shared below is a list of data validations operations that can be implemented in DsExcel Java.

* [Add Whole Number Validation](/document-solutions/java-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-whole-number-validation)
* [Add Decimal Validation](/document-solutions/java-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-decimal-validation)
* [Add List Validation](/document-solutions/java-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-list-validation)
* [Add Date Validation](/document-solutions/java-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-date-validation)
* [Add Time Validation](/document-solutions/java-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-time-validation)
* [Add Text Length Validation](/document-solutions/java-excel-api/docs/online/Features/ApplyDataValidations/AddValidations#add-text-length-validation)
* [Add Custom Validation](/document-solutions/java-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.

```Java
// Add whole number validation
worksheet.getRange("D2:E5").getValidation().add(ValidationType.Whole, ValidationAlertStyle.Stop,
        ValidationOperator.Between, 3, 8);
IValidation validation = worksheet.getRange("D2:E5").getValidation();
validation.setIgnoreBlank(true);
validation.setInputTitle("Tips");
validation.setInputMessage("Input a value between 3 and 8, please");
validation.setErrorTitle("Error");
validation.setErrorMessage("input value does not between 3 and 8");
validation.setShowInputMessage(true);
validation.setShowError(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.

```Java
// Add decimal validation 
worksheet.getRange("C2:E4").getValidation().add(ValidationType.Decimal, ValidationAlertStyle.Stop,
        ValidationOperator.Between, 111.5, 72.3);
```

## 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.

```Java
worksheet.getRange("A1").setValue("aaa");
worksheet.getRange("A2").setValue("bbb");
worksheet.getRange("A3").setValue("ccc");

// Use cell reference.
worksheet.getRange("C2:E4").getValidation().add(ValidationType.List, ValidationAlertStyle.Stop,
        ValidationOperator.Between, "=$a$1:$a$3", null);

// Or use string.
// this._worksheet.getRange("C2:E4").getValidation().add(ValidationType.List,ValidationAlertStyle.Stop,
// ValidationOperator.Between, "aaa, bbb, ccc",null);

IValidation validation = worksheet.getRange("C2:E4").getValidation();
validation.setInCellDropdown(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.

```Java
worksheet.getRange("C2:E4").getValidation().add(ValidationType.Date, ValidationAlertStyle.Stop,
        ValidationOperator.Between, new GregorianCalendar(2015, 11, 13), new GregorianCalendar(2015, 11, 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.

```Java
Calendar time1 = new GregorianCalendar(1899, 11, 30, 13, 30, 0);
Calendar time2 = new GregorianCalendar(1899, 11, 30, 18, 30, 0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
worksheet.getRange("C2:E4").getValidation().add(ValidationType.Time, ValidationAlertStyle.Stop,
        ValidationOperator.Between, simpleDateFormat.format(time1.getTime()),
        simpleDateFormat.format(time2.getTime()));
```

## 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.

```Java
worksheet.getRange("C2:E4").getValidation().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.

```Java
worksheet.getRange("A2").setValue(1);
worksheet.getRange("A3").setValue(2);
worksheet.getRange("C2").setValue(1);

// While using custom validation, validationOperator and formula2 parameters will be ignored even if you have provided.
worksheet.getRange("A2:A3").getValidation().add(ValidationType.Custom, ValidationAlertStyle.Information,
        ValidationOperator.Between, "=C2", null);
```

>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.