In this article I will explain with an example, how to validate at-least one TextBox from multiple TextBoxes in ASP.Net MVC.
The validation will be performed using Model and Data Annotations in ASP.Net 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
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult 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.
The Form
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
 
Implementing Validation
The Form consists of the following two HTML Helper functions:-
1. Html.TextBoxFor – Creating TextBox for the Model property.
2. Html.ValidationSummary – Displaying 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.Models.AddressProof
@{
    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>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table>
            <tr>
                <th colspan="2">Address Proofs (At-least one)</th>
            </tr>
            <tr>
                <td>Passport Number</td>
                <td>@Html.TextBoxFor(m => m.Passport)</td>
            </tr>
            <tr>
                <td>Aadhar Number</td>
                <td>@Html.TextBoxFor(m => m.Aadhar)</td>
            </tr>
            <tr>
                <td>PAN Number</td>
                <td>@Html.TextBoxFor(m => m.PAN)</td>
            </tr>
            <tr>
                <td colspan="2">@Html.ValidationSummary()</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    }
</body>
</html>
 
 
Screenshot
ASP.Net MVC: Validate At-least one TextBox from Multiple TextBoxes
 
 
Downloads