Introduction
One of the most advanced features of Aspose.Grid.Web is its capability to add input validation rules for cells. Developers can create different types of validation rules for cells to control and validate their input values. In this topic, we will discuss in detail about the types of validations supported by Aspose.Grid.Web and how can we create them.
Types of Validations
There are three types of validations that can be created by developers using Aspose.Grid.Web as follows:
- List Validation
- DropDownList Validation
- Custom Expression Validation
Let's discuss each type of validation in detail one by one.
List Validation
List Validation allows users to provide input for cells by either typing or selecting a value from the menu. To create a list validation for a cell, please follow the steps below:
- Add Aspose.Grid.Web control to your Web Form
- Access a desired worksheet
- Access a desired cell in which Validation is to be created
- Create Validation for the cell and specify the Validation Type to List
- Add Values for the List Validation
Example:
[C#]
//Accessing the cells collection of the worksheet that is currently active
WebWorksheet sheet=GridWeb1.WebWorksheets[GridWeb1.ActiveSheetIndex];
//Accessing "B1" cell
WebCell cell=sheet.Cells[0,1];
//Putting value to "B1" cell
cell.PutValue("Select Course:");
//Accessing "C1" cell
cell = sheet.Cells[0, 2];
//Creating List validation for the "C1" cell
cell.CreateValidation(ValidationType.List, true);
//Adding values to List validation
cell.Validation.ValueList.Add("Fortran");
cell.Validation.ValueList.Add("Pascal");
cell.Validation.ValueList.Add("C++");
cell.Validation.ValueList.Add("Visual Basic");
cell.Validation.ValueList.Add("Java");
cell.Validation.ValueList.Add("C#");
[VB.NET]
'Accessing the cells collection of the worksheet that is currently active
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(GridWeb1.ActiveSheetIndex)
'Accessing "B1" cell
Dim cell As WebCell = sheet.Cells(0,1)
'Putting value to "B1" cell
cell.PutValue("Select Course:")
'Accessing "C1" cell
cell = sheet.Cells(0, 2)
'Creating List validation for the "C1" cell
cell.CreateValidation(ValidationType.List, True)
'Adding values to List validation
cell.Validation.ValueList.Add("Fortran")
cell.Validation.ValueList.Add("Pascal")
cell.Validation.ValueList.Add("C++")
cell.Validation.ValueList.Add("Visual Basic")
cell.Validation.ValueList.Add("Java")
cell.Validation.ValueList.Add("C#")
The above code will add a List Validation to C1 cell. When a user would click on the C1 cell, a list would appear and users can either type or select a value from the menu as shown below:
|
Figure: Selecting a value from the List
|
DropDownList Validation
DropDownList Validation allows users to provide input for cells by only selecting a value from the pre-defined list of values. To create a DropDownList validation for a cell, please follow the steps below:
- Add Aspose.Grid.Web control to your Web Form
- Access a desired worksheet
- Access a desired cell in which Validation is to be created
- Create Validation for the cell and specify the Validation Type to DropDownList
- Add Values for the DropDownList Validation
Example:
[C#]
//Accessing the cells collection of the worksheet that is currently active
WebWorksheet sheet=GridWeb1.WebWorksheets[GridWeb1.ActiveSheetIndex];
//Accessing "B1" cell
WebCell cell=sheet.Cells[0,1];
//Putting value to "B1" cell
cell.PutValue("Select Degree:");
//Accessing "C1" cell
cell = sheet.Cells[0, 2];
//Creating DropDownList validation for the "C1" cell
cell.CreateValidation(ValidationType.DropDownList, true);
//Adding values to DropDownList validation
cell.Validation.ValueList.Add("Bachelor");
cell.Validation.ValueList.Add("Master");
cell.Validation.ValueList.Add("Doctor");
[VB.NET]
'Accessing the cells collection of the worksheet that is currently active
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(GridWeb1.ActiveSheetIndex)
'Accessing "B1" cell
Dim cell As WebCell = sheet.Cells(0,1)
'Putting value to "B1" cell
cell.PutValue("Select Degree:")
'Accessing "C1" cell
cell = sheet.Cells(0, 2)
'Creating DropDownList validation for the "C1" cell
cell.CreateValidation(ValidationType.DropDownList, True)
'Adding values to DropDownList validation
cell.Validation.ValueList.Add("Bachelor")
cell.Validation.ValueList.Add("Master")
cell.Validation.ValueList.Add("Doctor")
The above code will add a DropDownList Validation to C1 cell. When a user would click on the C1 cell, a DropDownList would appear and users can select a value from the list as shown below:
|
Figure: Selecting a value from DropDownList
|
Custom Expression Validation
Custom Expression Validation allows developers to write their own Custom Regular Expressions to validate the input values of end users. To create a custom expression validation for a cell, please follow the steps below:
- Add Aspose.Grid.Web control to your Web Form
- Access a desired worksheet
- Access a desired cell in which Validation is to be created
- Create Validation for the cell and specify the Validation Type to CustomExpression
- Set Regular Expression for the Custom Expression Validation
Example:
[C#]
//Accessing the cells collection of the worksheet that is currently active
WebWorksheet sheet=GridWeb1.WebWorksheets[GridWeb1.ActiveSheetIndex];
//Accessing "B1" cell
WebCell cell=sheet.Cells[0,1];
//Putting value to "B1" cell
cell.PutValue("Date (yyyy-mm-dd):");
//Accessing "C1" cell
cell = sheet.Cells[0, 2];
//Creating a custom expression validation for the "C1" cell
cell.CreateValidation(ValidationType.CustomExpression, true);
//Setting regular expression for the validation to accept dates in yyyy-mm-dd format
cell.Validation.RegEx = @"\d{4}-\d{2}-\d{2}";
[VB.NET]
'Accessing the cells collection of the worksheet that is currently active
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(GridWeb1.ActiveSheetIndex)
'Accessing "B1" cell
Dim cell As WebCell = sheet.Cells(0,1)
'Putting value to "B1" cell
cell.PutValue("Date (yyyy-mm-dd):")
'Accessing "C1" cell
cell = sheet.Cells(0, 2)
'Creating a custom expression validation for the "C1" cell
cell.CreateValidation(ValidationType.CustomExpression, True)
'Setting regular expression for the validation to accept dates in yyyy-mm-dd format
cell.Validation.RegEx = "\d{4}-\d{2}-\d{2}"
The above code will add a Custom Expression Validation to C1 cell for the users to add Date into C1 cell according to the format specified in regular expression as shown below:
|
Figure: Adding a Date value to C1 cell according to Regular Expression
|
Forcing Validation
Using Aspose.Grid.Web, users may post input data to server. Even you have created validation rules for different cells but if ForceValidation property of GridWeb control is not set to true then wrong input data will also be submitted to server by Aspose.Grid.Web and no validation would be forced on end users. ForceValidation property of GridWeb is always set to true by default.
If ForceValidation property of GridWeb control is set to true then control will not post data to the web server until the input values of all cells are valid. For example, if we enter a wrong input value into cell or simply don't enter a value then client side validation would get activated and end users would not be able to post data to server even they click Submit button as shown below:
|
Figure: Wrong input value highlighted by GridWeb
|