Search code examples
jqueryvalidationasp.net-mvc-3

JQuery Validate Options not working MVC3 Razor


I am trying to get a simple ignore working for the JQuery validate and I dont seem to be able to get it to work. No matter what I do it seems to not use the options. I have trimmed the code down and pasted below.

I am using MVC3 Razor and JQuery 1.5.1

@Code
    ViewData("Title") = "Index"
    Layout = "~/Areas/Quote/Views/Shared/_Layout.vbhtml"
End Code
<script type="text/javascript">
    $(document).ready(function () {
        $('#myform').validate({
            ignore: "#textx"
        })
    });
</script>
@Using Html.BeginForm("Index", "YourQuote", FormMethod.Post, New With {.id = "myform"})
    @<input  type="text" id="textx" class="required"/>
    @<input type="submit" value="save" />
End Using

The example is simplified but what I really want to do is have an overriding rule which says ignore all hidden elements from validation. My page is dynamically built and removing class and adding them back in when parent elements are hidden or shown is not good for me

@Code
    ViewData("Title") = "Index"
    Layout = "~/Areas/Quote/Views/Shared/_Layout.vbhtml"
End Code
<script type="text/javascript">
    $(document).ready(function () {
        $('#myform').validate({
            ignore: ":hidden"
        })
    });
</script>
@Using Html.BeginForm("Index", "YourQuote", FormMethod.Post, New With {.id = "myform"})
    @<div style="display:none">
    <input  type="text" id="textx" class="required"/>
    </div>
    @<input type="submit" value="save" />
End Using

Solution

  • Try removing the unobtrusive jquery inclusion, and just include jquery.validate. I had a similar problem and somehow the unobtrusive library screwed up my custom defined settings