In this article I will explain with an example, how to validate at-least one TextBox from multiple TextBoxes in ASP.Net Core MVC.
The validation will be performed using Model and Data Annotations in ASP.Net Core MVC.
 
 
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; }
}
 
 
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action 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 HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(AddressProof addressProof)
    {
        if (ModelState.IsValid)
        {
            // Validation success.
        }
 
        return View();
    }
}
 
 
View
Inside the View, in the very first line the AddressProof class is declared as Model for the View and the ASP.Net TagHelpers is inherited.
The Form
The View consists of an HTML Form which has been created using following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
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 and a HTML DIV element.
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.
There is also a Submit button which when clicked, the Form gets submitted and the AddressProof class is sent to the Controller.
@model Validate_AtLeast_TextBox_DataAnnotation_MVC_Core.Models.AddressProof
@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" asp-controller="Home" asp-action="Index">
        <table>
            <tr>
                <th colspan="2">Address Proofs (At-least one)</th>
            </tr>
            <tr>
                <td>Passport Number</td>
                <td><input type="text" asp-for="Passport" /></td>
            </tr>
            <tr>
                <td>Aadhar Number</td>
                <td><input type="text" asp-for="Aadhar" /></td>
            </tr>
            <tr>
                <td>PAN Number</td>
                <td><input type="text" asp-for="PAN" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <div asp-validation-summary="All"></div>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
 
 
Screenshot
ASP.Net Core: Validate At-least one TextBox from Multiple TextBoxes
 
 
Downloads