0

I have an ASP.NET MVC site that uses Data Annotations for validations and I'd like to get "free" client-side validations. Most simple ones work well and Foolproof helps me with more complex scenarios. All is good so far.

However, I'd like to tie into HTML5 validations with browser support. Specifically, I want to use the little popups for all of my client-side validation messages.

I've created a JSFiddle example here explaining what I want and am coming from: https://jsfiddle.net/4nftdufu/

The behavior I want to see is shown by the first form (Foo).

<form class="form-inline"> <div class="form-group"> <input type="text" class="form-control" id="inputFoo" placeholder="Type Foo Here" required> </div> <button type="submit" class="btn btn-primary">Submit Foo</button> </form> 

The second form (Bar) is essentially where I'm coming from. Note that I'm hooking into some Bootstrap validation behavior here (I found that CSS online somewhere in a blog post or some other SO question). Ultimately, this is not the behavior I want and I've not spent any time cleaning up this imperfect integration.

<form class="form-inline"> <div class="form-group"> <input class="form-control input-validation-error" data-val="true" data-val-required="Required" id="inputBar" name="inputBar" placeholder="Enter Bar here" type="text" value="" aria-required="true" aria-describedby="Bar-error" aria-invalid="true"> <p class="help-block"><span class="field-validation-valid" data-valmsg-for="inputBar" data-valmsg-replace="true"></span></p> </div> <button type="submit" class="btn btn-primary">Submit Bar</button> </form> 

How can I get my Data Annotations + jQuery Unobtrusive-driven validations to hook into these HTML popups for all validation messages when in a supported browser?

8
  • 1
    Your first example is leveraging the browser's validation, not anything to do with bootstrap. At least, when I run the fiddle that's what I see. It happens because it sets required attribute directly in the input. The second example is leveraging standard jquery validation.
    – steve v
    CommentedSep 15, 2017 at 15:06
  • 1
    See also: stackoverflow.com/questions/26531033/…
    – steve v
    CommentedSep 15, 2017 at 15:08
  • @stephen.vakil Really? I had just assumed that was Bootstrap somehow driving it. I didn't realize such behavior was in browsers today! So, are there any good ways to hook my Data Annotation validations into that? Or is that a bad idea for some reason?
    – Jaxidian
    CommentedSep 15, 2017 at 15:35
  • I've fixed my question to remove my incorrect assumptions about bootstrap. Deleted some text but also left some stricken in case others have the same assumption I had.
    – Jaxidian
    CommentedSep 15, 2017 at 21:36
  • 1
    You cant. Using client side validation with jquery.validate.unobtrusive.js adds the novalidate attribute to your form so that HTML5 validation is not performed (because they do not work together)
    – user3559349
    CommentedSep 15, 2017 at 21:56

1 Answer 1

1

MVC's client side validation using the jquery.validate.js and jquery.validate.unobtrusive.js files and HTML-5 validation do not play well together. The jquery.validate.js file in fact adds the novalidate attribute to your <form> element to disable HTML-5 validation using the following code

// Add novalidate tag if HTML5. this.attr( "novalidate", "novalidate" ); 

If you want your messages to look like the browsers callouts, then you can always use css to style the element generated by @Html.ValidationMessageFor(). When a form control is invalid, a class="field-validation-error" is added to the element which can be used for styling (adding color, borders, using the ::after pseudo selector to add arrows etc)

1
  • Thanks. This isn't the answer I was hoping for but I do believe it answers my question in that the jQuery Unobtrusive validations are incompatible with the built-in HTML5 validations (which I had previously confused as being Bootstrap-related). Thank you for clarifying this for me!
    – Jaxidian
    CommentedSep 19, 2017 at 13:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.