ASP.NET Core Form Validation with HTML5 (No jQuery)
ASP.NET Core form validation with HTML5 attributes instead of jQuery: how asp-validation-for works, which data annotations map to which attributes, and setup.
Updated November 2026 for .NET 10 and Finbuckle.Html5Validation 10.0.1.
ASP.NET Core form validation starts with data annotations on your model. The server checks them into ModelState, and the tag helpers also render data-val-* attributes that jQuery Validation and jQuery Unobtrusive Validation read in the browser. You can replace that client-side half with native HTML5 attributes such as required, min, max, and pattern, which drops three JavaScript dependencies and plays well with libraries like htmx. Server-side validation stays exactly as it is, and it is the part you can never skip.
I ran into this while building an htmx and ASP.NET Core demo. I wanted to limit client-to-server requests to valid requests, and htmx does that for free when your inputs use HTML5 validators. The default ASP.NET Core attributes aren’t HTML5, so I looked for a way to change that. Luckily, OSS author Andrew White published the Finbuckle.Html5Validation NuGet package.
How does form validation work in ASP.NET Core?
By default, validation in ASP.NET Core MVC and Razor Pages has three moving parts:
- Data annotations on the model. Attributes such as
[Required],[StringLength],[Range],[EmailAddress], and[RegularExpression]describe the rules. - Server-side validation. During model binding, ASP.NET Core runs those rules and records failures in
ModelState. You checkModelState.IsValidin your handler. - Client-side validation. The
inputtag helper reads the same annotations and writesdata-val-*attributes into the HTML. The jQuery Validation and jQuery Unobtrusive Validation scripts read those attributes in the browser. According to Microsoft Learn, this saves you from writing the same logic twice, once in C# and once in JavaScript.
Here is a model with a few annotations on a Razor Page.
using System.ComponentModel.DataAnnotations;using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.RazorPages;
namespace FormValidation.Pages;
public class IndexModel : PageModel{ [BindProperty, Required] public string? Name { get; set; }
[BindProperty, EmailAddress] public string? Email { get; set; }
[BindProperty, StringLength(20, MinimumLength = 3)] public string? Handle { get; set; }
[BindProperty, Range(18, 120)] public int Age { get; set; }
[BindProperty, RegularExpression("^[A-Z]{3}$", ErrorMessage = "Use three capital letters.")] public string? Code { get; set; }
public void OnGet() { }
public IActionResult OnPost() { if (!ModelState.IsValid) { return Page(); }
return RedirectToPage(); }}With no extra configuration, the Name property renders like this. I captured this output from a .NET 10 Razor Pages app.
<input type="text" data-val="true" data-val-required="The Name field is required." id="Name" name="Name" value="" /><span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>Everything starting with data-val is an ASP.NET Core invention. Browsers ignore it, and only the jQuery scripts act on it.
What does asp-validation-for do?
asp-validation-for is a tag helper for a <span> that tells ASP.NET Core where to show the validation message for one model property. For <span asp-validation-for="Name"></span>, it renders a span with the data-valmsg-for="Name" attribute. Per Microsoft Learn, when a client-side validation error occurs, jQuery displays the error message in that span. After a failed POST, the server renders the ModelState error for Name inside the same span and gives it the field-validation-error class.
@page@model FormValidation.Pages.IndexModel
<form method="post"> <div asp-validation-summary="ModelOnly"></div>
<label asp-for="Name"></label> <input asp-for="Name" /> <span asp-validation-for="Name"></span>
<button type="submit">Save</button></form>After posting an empty Name to the page, the server returned this span (observed output):
<span class="field-validation-error" data-valmsg-for="Name" data-valmsg-replace="true">The Name field is required.Its sibling, asp-validation-summary, goes on a <div> and displays a summary of validation messages. According to Microsoft Learn, All shows property and model-level messages, ModelOnly shows model-level messages only, and None shows none. ModelOnly pairs well with per-field spans because the field messages do not repeat in the summary.
The key point for this post is that asp-validation-for still works with HTML5 validation. The span is how the server shows errors on a re-rendered page. It does not depend on jQuery to do that job.
What is HTML5 validation?
Before HTML5, developers wrote all client-side validation in JavaScript. While JavaScript code allowed for complex scenarios, many folks realized two things: client-side validation has many recurring patterns, and, ultimately, it is not a bulletproof validation approach. Server-side validation is essential.
This realization helped HTML narrow the scope of client-side validation, limiting requests sent to the server to client-validated ones. The validation focuses on a few user input characteristics:
- Required: This field must have some value.
- Type: This field conforms to the expected integer, date, phone number, etc.
- Constraints: This field has a minimum, maximum, or fits within a specified range.
- Pattern: This field conforms to a regular expression pattern
These agreed-upon validations reduce the required JavaScript code while allowing clients to create native experiences based on the input types.
Let’s take a look at an example of an HTML5-validated form. The following code can be seen on the MDN site.
<form> <fieldset> <legend> Do you have a driver's license?<span aria-label="required">*</span> </legend> <input type="radio" required name="driver" id="r1" value="yes" /><label for="r1" >Yes</label > <input type="radio" required name="driver" id="r2" value="no" /><label for="r2" >No</label > </fieldset> <p> <label for="n1">How old are you?</label> <input type="number" min="12" max="120" step="1" id="n1" name="age" pattern="\d+" /> </p> <p> <label for="t1" >What's your favorite fruit?<span aria-label="required">*</span></label > <input type="text" id="t1" name="fruit" list="l1" required pattern="[Bb]anana|[Cc]herry|[Aa]pple|[Ss]trawberry|[Ll]emon|[Oo]range" /> <datalist id="l1"> <option>Banana</option> <option>Cherry</option> <option>Apple</option> <option>Strawberry</option> <option>Lemon</option> <option>Orange</option> </datalist> </p> <p> <label for="t2">What's your email address?</label> <input type="email" id="t2" name="email" /> </p> <p> <label for="t3">Leave a short message</label> <textarea id="t3" name="msg" maxlength="140" rows="5"></textarea> </p> <p> <button>Submit</button> </p></form>As you’ll notice, the attributes required, pattern, min, max, and type create a validation experience with no lines of JavaScript. When you attempt to submit the form, the client displays a message. You can also highlight inputs based on their validation status.
Awesome, right?! With a bit of CSS, we can get some UI indicators. Depending on the client, any input with an appropriate type automatically gets access to the native type value picker, whether it’s a date, a phone number, or a number.
How is HTML5 validation different from the default?
The default approach describes rules in custom attributes and relies on a script to enforce them. HTML5 describes rules in standard attributes that the browser enforces itself. All attribute rows below come from rendering the model above (plus a few more properties) in a .NET 10 app, once with defaults and once with AddHtml5Validation().
| Default (jQuery Unobtrusive) | Native HTML5 (Finbuckle.Html5Validation) | |
|---|---|---|
| Dependencies | jquery, jquery-validation, jquery-validation-unobtrusive (the Razor Pages template ships them under wwwroot/lib) |
None |
| Where rules live | data-val-* attributes |
Standard HTML attributes |
| Where messages come from | The data-val-* attribute text, shown by jQuery in the asp-validation-for span |
The browser’s built-in UI; the title attribute carries your message when the annotation has an ErrorMessage, which per the package README browsers show as a tooltip |
[Required] |
data-val-required |
required |
[StringLength(20, MinimumLength = 3)] |
data-val-length, maxlength |
minlength, maxlength |
[MinLength(2)] |
data-val-minlength |
minlength |
[MaxLength(10)] |
data-val-maxlength, maxlength |
maxlength |
[Range(18, 120)] |
data-val-range, data-val-range-min, data-val-range-max |
min, max |
[RegularExpression] |
data-val-regex, data-val-regex-pattern |
pattern |
[EmailAddress] |
type="email", data-val-email |
type="email" |
[Phone] |
type="tel", data-val-phone |
type="tel" |
[Url] |
type="url", data-val-url |
type="url" |
| htmx | htmx checks the HTML5 Validation API, and data-val attributes are not HTML5 constraints (the default output had no required on Name), so there is nothing for htmx to check beyond native attributes like type and maxlength |
htmx integrates with the HTML5 Validation API and does not issue a request for a form with an invalid input |
| Server-side validation | Unchanged | Unchanged |
A few details I saw in the output are worth knowing:
- A non-nullable
intsuch asAgeis treated as required even without[Required]: it getsdata-val-requiredin the default mode andrequiredwith the package. - A
[DataType(DataType.Date)]property renderstype="date"in both modes. - The package’s README says
titleis added only when you set an explicitErrorMessage. In my output,[EmailAddress],[Phone], and[Url]also got atitleeven though I didn’t set one. The reason is that those three attributes set a defaultErrorMessagein their constructors (new EmailAddressAttribute().ErrorMessagereturnsThe {0} field is not a valid e-mail address., whilenew RequiredAttribute().ErrorMessagereturnsnull).
How do I turn off the default client-side validation?
Set ClientValidationEnabled to false on the HTML helper options. Microsoft Learn documents the same call.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages() .AddViewOptions(options => { options.HtmlHelperOptions.ClientValidationEnabled = false; });
var app = builder.Build();
app.MapRazorPages();
app.Run();This stops Razor Pages and MVC from writing the data-val attributes. With it on, the same Name input is rendered like this:
<input type="text" id="Name" name="Name" value="" />The asp-validation-for span also lost its data-valmsg-for and data-valmsg-replace attributes in my output. Attributes that come from the input’s type or length, such as type="email" and maxlength="20", stayed.
Disabling alone gives you no HTML5 rules either, so you write required, min, pattern, and friends by hand. That suits folks who prefer to keep models and views separate. I think the next section is the better option for most folks, since it still relies on the model metadata that ASP.NET Core provides.
How do I use Finbuckle.Html5Validation?
Install the package. As of this update, the latest version is 10.0.1, published 2026-06-20 and targeting net10.0. Earlier versions target older frameworks, so match the package to your target framework.
dotnet add package Finbuckle.Html5ValidationThis package only works with MVC and Razor Pages. Next, register it with your services.
using Finbuckle.Html5Validation;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();builder.Services.AddHtml5Validation();
var app = builder.Build();
app.MapRazorPages();
app.Run();Internally, the package supplies the ASP.NET Core pipeline with its own IValidationAttributeAdapterProvider. Those adapters decide which HTML attributes each data annotation produces, which is why you do not need to change your Razor views. The same IndexModel now renders these inputs:
<input type="text" id="Name" name="Name" required="" value="" /><input type="text" id="Handle" maxlength="20" minlength="3" name="Handle" value="" /><input type="number" id="Age" max="120" min="18" name="Age" required="" value="0" /><input type="text" id="Code" name="Code" pattern="^[A-Z]{3}$" title="Use three capital letters." value="" />No data-val attributes remain, so you can delete the jQuery validation scripts. Because the rules are standard attributes, you can use them with htmx. I’m using htmx here, hence the hx- attributes.
@page@model FormValidation.Pages.IndexModel
<form method="post" asp-page="Index" hx-post="@Url.Page("Index")" hx-swap="outerHTML">
<label asp-for="Name"></label> <input asp-for="Name" /> <span asp-validation-for="Name"></span>
<label asp-for="Age"></label> <input asp-for="Age" />
<button type="submit">Save</button></form>Browsers expose validity to CSS, so you can style invalid fields too.
input:user-invalid { border-color: red; background-color: pink; box-shadow: 0 0 5px 1px red;}You can target several states, such as :valid, :invalid, and :user-invalid. The :user-invalid state only triggers after a user has interacted with an element, whereas you can use the :invalid state to signify a field is invalid immediately.
Let’s run our application and submit the form.
Sweet! All looks like it’s working as expected. For more native inputs that work with ASP.NET Core model binding, see my posts on range inputs and tag helpers and datalist inputs, and my HTMX with ASP.NET Core introduction.
Does client-side validation replace server-side validation?
No, and this applies to both approaches. Client-side validation, whether jQuery or HTML5, is a convenience that saves a round trip. Requests do not have to come from your form (my curl test below is one), so the server must validate every request. Microsoft Learn says the same about remote validation: if the client-side check is required for processing, always implement separate server-side validation.
I checked this with the HTML5 package enabled by posting Name=&Age=5 with curl, which skips the browser entirely. The server answered 200 and re-rendered the page with The Name field is required. in the asp-validation-for span. In OnPost, ModelState.IsValid is false in that case, so the handler returns Page() instead of saving.
FAQ
Can I use HTML5 validation with ASP.NET Core data annotations?
Yes. Add the Finbuckle.Html5Validation package and call AddHtml5Validation(). Your annotations stay as they are, and the tag helpers emit HTML5 attributes instead of data-val attributes.
Do I still need jQuery for ASP.NET Core form validation?
Not for client-side checks if you use native HTML5 attributes. The default data-val approach does need jQuery, jQuery Validation, and jQuery Unobtrusive Validation.
What does asp-validation-for do?
It renders a <span> bound to one model property. That span holds the error message for that property, whether jQuery or the server fills it in.
Does HTML5 validation work with htmx? Yes. According to the htmx docs, htmx integrates with the HTML5 Validation API and will not issue a request if a validatable input is invalid.
Conclusion
While validation libraries can offer a lot in terms of user experience, native validation is great. In fact, it’s good enough to handle most client-side validation, and you can then lean on server-side validation to handle the rest.
I recommend trying this approach and seeing how far you get. You’ll likely be surprised how many dependencies you can drop in favor of native controls and validation.
As always, thanks for reading and sharing my posts. Cheers.
Around the web
Comments
Loading comments from GitHub Discussions…