In this article I will explain with an example, how to implement TextBox validation in ASP.Net Core.
The TextBox validation will be performed on Client Side as well as Server Side using Data Annotations in ASP.Net Core.
Note: For beginners in ASP.Net Core 7, please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

Model

The Model class consists of following property Gender. The property is decorated with the following Data Annotation attributes for performing validation.
1. Required Data Annotation attribute.
Note: The Data Annotations attributes can be used with the Entity Data Model (EDM), LINQ to SQL, and other data models.
 
The Required Data Annotation 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.
public class PersonModel
{
    [Required(ErrorMessage = "Name is required.")]
    public string Name { get; set; }
}
 
 

Controller

The Controller consists of following 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 is called when the form is submitted which accepts the PersonModel class object as parameter.
The state of the submitted Model is checked using ModelState.IsValid property.
Then, a check is performed if the ModelState is valid then the value of the Name property is set into a ViewBag object.
Finally, the View is returned.
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(PersonModel person)
    {
        if (ModelState.IsValid)
        {
            ViewBag.Name = person.Name;
        }
 
        return View();
    }
}
 
 

View

Inside the View, in the very first line the PersonModel class is declared as Model for the View.
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.
 

Implementing Validation

The Form consists of an HTML INPUT TextBox, a SPAN and a Submit Button.
The TextBox has been set with the following Tag Helpers attribute:-
asp-for – The Model property to which validation will be performed. In this case Name.
The HTML SPAN has been set with the following Tag Helpers attribute:-
asp-validation-for – Displaying the validation message for the Model property.
 

Enabling Client-Side validations

By default, the validations performed using Data Annotations and Model class is performed on Server Side.
In order to enable Client-Side validations, you will need to inherit the following script files.
1. jquery.min.js
2. jquery.validate.js
3. jquery.validate.unobtrusive.js
Once the above files are inherited automatically, the Client-Side validations using Data Annotations is enabled.
 

Submitting the Form

When the Submit button is clicked, the Form gets submitted and the Name value is sent to the Controller.
Finally, a check is performed for Model and if the Model is valid, then the value of the ViewBag object is displayed.
@model TextBox_Validation_MVC_Core.Models.PersonModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        <table>
            <tr>
                <td><input asp-for="Name" /></td>
                <td><span asp-validation-for="Name" class="error"></span></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit" /></td>
                <td></td>
            </tr>
        </table>
    </form>
    <hr />
    @if (ViewData.ModelState.IsValid)
    {
        @ViewBag.Name;
    }
    <!--OPTIONAL: Add the following scripts for enabling Client Side validation.-->
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.js"></script>
</body>
</html>
 
 

Screenshot

TextBox validation in ASP.Net Core
 
 

Downloads