In this article I will explain with an example, how to validate at-least one TextBox from multiple TextBoxes in ASP.Net Core Razor Pages.
The validation will be performed using Model and Data Annotations in ASP.Net Core Razor Pages.
Custom Validation Data Annotations Attribute for Multiple TextBoxes
For validating multiple TextBoxes, a new class will be created for developing Custom Validation Data Annotations attribute.
This attribute will check all the properties in the Model class and validate whether if one of the property has value or not. If none of the property has value will raise the error.
using System.Reflection;
using System.ComponentModel.DataAnnotations;
public class AtLeastOneRequired : ValidationAttribute
{
public override bool IsValid(object value)
{
// Get Type of value.
Type typeInfo = value.GetType();
// Get PropertyInfo of Type using Reflection.
PropertyInfo[] propertyInfos = typeInfo.GetProperties();
// Loop through PropertyInfo Array.
foreach (PropertyInfo propertyInfo in propertyInfos)
{
// Check if property has value or not.
if (propertyInfo.GetValue(value) != null)
{
// Property has value.
return true;
}
}
// All Properties are NULL.
return false;
}
}
Model
The following Model class consists of three properties and has been specified with the Custom Validation Data Annotations attribute.
Note: The Data Annotations attributes can be used with the Entity Data Model (EDM), LINQ to SQL, and other data models.
The Custom Validation Data Annotations attribute has been specified with a property ErrorMessage with a string value. As the name suggests, this string value will be displayed to the user when the validation fails.
[AtLeastOneRequired(ErrorMessage = "Please enter at-least one Address proof.")]
public class AddressProof
{
public string Passport { get; set; }
public string Aadhar { get; set; }
public string PAN { get; set; }
}
Razor PageModel (Code-Behind)
The PageModel consists of following Handler methods.
Handler method for handling GET operation
This Handler method handles the GET calls, for this particular example it is not required and hence left empty.
Handler method for handling POST operation
This Handler method handles the POST operation and when the form is submitted, the object of the AddressProof class is sent to this method.
The state of the submitted Model is checked using ModelState.IsValid property.
Note: ModelState.IsValid property is an inbuilt property which verifies two things:
1. Whether the Form values are bound to the Model.
2. All the validations specified inside Model class using Data annotations have been passed.
public class IndexModel : PageModel
{
public AddressProof AddressProof { get; set; }
public void OnGet()
{
}
public void OnPostSubmit(AddressProof addressProof)
{
if (ModelState.IsValid)
{
// Validation success.
}
}
}
Razor Page (HTML)
Inside the Razor Page, the ASP.Net TagHelpers is inherited.
The Form
The HTML of Razor Page consists of an HTML Form which has been created using following ASP.Net Tag Helpers attribute.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of multiple HTML TextBoxes, a HTML DIV element and a Submit Button.
The HTML TextBoxes are assigned with asp-for attribute, while HTML DIV is assigned with asp-validation-summary attribute which displays the validation message for the properties when the validation fails.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute. When the Submit button is clicked, the Form gets submitted and the AddressProof class is sent to the Handler method.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
@page
@model Validate_AtLeast_TextBox_DataAnnotation_Razor_Core.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
.validation-summary-errorsul { color: Red; list-style: none; display: table-cell; padding-left: 0; }
</style>
</head>
<body>
<form method="post">
<table>
<tr>
<th colspan="2">Address Proofs (At-least one)</th>
</tr>
<tr>
<td>Passport Number</td>
<td><input type="text" asp-for="AddressProof.Passport" /></td>
</tr>
<tr>
<td>Aadhar Number</td>
<td><input type="text" asp-for="AddressProof.Aadhar" /></td>
</tr>
<tr>
<td>PAN Number</td>
<td><input type="text" asp-for="AddressProof.PAN" /></td>
</tr>
<tr>
<td colspan="2">
<div asp-validation-summary="All"></div>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" asp-page-handler="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Screenshot
Downloads