Validation is a necessary part of any application. There may be many inbuilt validations in an application. If you want to use a custom validation to ensure useful data in an application then you can do that. There may be many custom validations that can be created in LightSwitch, but here we will discuss some custom validations which are as follows.
Step 1 : Open Visual Studio->Click on create new table.

Step 2 : Create a table, such as employees.

Step 3 : Go to properties of FirstName->Mark IsRequired checkbox and maximum length(255)->Click on custom validation->Write the code.

Code
partial
void FirstName_Validate(EntityValidationResultsBuilder
results)
{
//
results.AddPropertyError("<Error-Message>");
if (this.FirstName
!= null && !System.Text.RegularExpressions.Regex.IsMatch(this.FirstName,
@"^[a-zA-Z]+$"))
{
results.AddPropertyError("Invalid
characters, Only alphabets allowed.");
}
}
Step 4 : Go to properties of LastName->Mark IsRequired checkbox and maximum length(255)->Click on custom validation->Write the code.

Code
partial
void LastName_Validate(EntityValidationResultsBuilder
results)
{
//
results.AddPropertyError("<Error-Message>");
if (this.LastName
!= null && !System.Text.RegularExpressions.Regex.IsMatch(this.LastName,
@"^[a-zA-Z]+$"))
{
results.AddPropertyError("Invalid
characters, Only alphabets allowed.");
}
}
Step 5 : Go to DateOfBirth properties->Click on custom validation.

Code
partial
void DateOfBirth_Validate(EntityValidationResultsBuilder
results)
{
//
results.AddPropertyError("<Error-Message>");
if (this.DateOfBirth.CompareTo(this.JoiningDate)
> 0)
{
results.AddPropertyError("DateOfBirth
should be earlier than joining date");
}
}








Join the conversation! Your thoughts help the community grow.