Is there any mode to use Asp.Net Core client side validation without jquery + jquery validation + jq unobtrusive validation? I mean it's 2017, every browser can handle lots of HTML5 input validators without JavaScript.
3 Answers
I created a library that is compatible with the original validation library but is much smaller and does not require jQuery. You can use it as a drop-in replacement if you want to reduce payload size of your assets:
Comments
This library seems to be more feature complete as a drop-in replacement for JQuery unobtrusive validation:
https://github.com/haacked/aspnet-client-validation
https://www.npmjs.com/package/aspnet-client-validation?activeTab=readme
Of particular interest, to me, in this library was the use of a MutationObserver to watch for DOM changes. This allows you to dynamically add fields and have their validation automatically wired up!
It also seems to have been updated more recently, as of May 2023, than the other library listed in this thread.
1 Comment
I don't know of any package that turns TagHelper for input fields into html5 element tags. You could probably quite easily make one. Check out the source code of the tag helpers: https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.TagHelpers/src/InputTagHelper.cs
Advise
Best approach would be to make sure you receive a json error object when validation fails and process this in javascript to show errrors (maybe show which fields fail). make a generic function you can use for any form.
Later manually fine tune/add additional tags/validation if needed in frontend. but that shouldn't be part of your MVP (minimal viable product).
old answer
Depends on what kind of validation you are looking for.
jQuery validation in combination with data annotations is the easiest one and the default one. Although html 5 can tackle quite some extensive tests. it can't handle any indepth stuff like properties being dependent on eachother.
Another option is to use the Popular FluentValidation. Validate only serverside but use Ajax and a small client side framework to show the errors
The last option would be to use Html5 input validations. This can only be partly achieved by default using dotnet types or some annotations: http://www.davepaquette.com/archive/2015/05/13/mvc6-input-tag-helper-deep-dive.aspx. You'd have to extend the taghelpers yourself if you'd want to add more in depth html 5 attribute support.
Too bad you can't use the same frontend and serverside code (like nodejs could) but for big projects i'd advise you to take option 2. it might not sound very snappy but dotnet is generally pretty fast and if you use FluentValidation in combination with Ajax Api calls no one will notice validation actually serverside. In the end i found data annotations always lack proper possibilities to test properties dependent on each other and it gets very hacky very fast. The only downside of this option is that you split the validation from the input-model and thus not always the validation is directly visible for external developers.