In this article I will explain with an example, how to validate minimum character length in ASP.Net Core.
Regular Expression (Regex) to validate minimum character length
^[\s\S]{8,}$
Model
The following Model class consists of one property Name. The property is decorated with the following Data Annotation attribute for performing validation.
1. RegularExpression 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
RegularExpression Data Annotation attribute have 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 respective validation fails.
The following conditions must satisfy for a Name to be termed as valid.
1. It should be 8 characters long.
2. Matches any whitespace character.
Valid example: ASPSNIPPETS.
using System.ComponentModel.DataAnnotations;
namespace MinValidation_Core.Models
{
public class Person
{
[RegularExpression(@"^[\s\S]{8,}$", ErrorMessage = "Minimum 8 characters 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 handles the POST operation and when the form is submitted, the object of the Person model 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(Person person)
{
if (ModelState.IsValid)
{
// Validation success.
}
return View();
}
}
View
Inside the View, in the very first line the Person model 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.
The Form consists of a HTML INPUT TextBox, a SPAN element 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.
Submitting the Form
When the Submit button is clicked, the Form gets submitted and the Name value is sent to the Controller.
@model MinValidation_Core.Models.Person
@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">
Name:
<input type="text" asp-for="Name" />
<span asp-validation-for="Name" class="error"></span>
<hr />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Screenshot
Downloads