views:

44

answers:

1

I'm trying to use the JQuery Validation's "addClassRules" method in my ASP.NET MVC application.

In my master page I'm including the following files...

    <script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMVCAjax.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMVCValidation.js") %>" type="text/javascript"></script>

<script src="<%= Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery-validate.js") %>" type="text/javascript"></script>

I'm not sure which are actually required so I've added them all:) Potential problem area?

In my View I've got a simple textbox element which I'm trying to add the "required" validation via the Class attribute. I've got dynamic forms and my end goal is to specify different validation rules in the Class attribute. So for now I'm got something like this...

    <%: Html.TextBoxFor(model => model.FieldEntry.Response.TextBoxValue, new { @class = "cRequired" } )%>

then at the end of my View I'm including the following script which I though was suppose to hook up the Class name to a validation rule.

<script language="javascript" type="text/javascript"> 
jQuery(document).ready( function()
{
    jQuery.validator.addClassRules({
      cRequired: {
        required: true
      }
    });
})
</script>

Seems like a straightforward example but the problem I am having is that I keep getting an error that jQuery.validator is null or not an object. This refers to the first line of the the ready() function in the script above.

Any help / suggestions?

+1  A: 

I believe the problem should be somewhere at including the validator plugin because jQuery.validator should not be null if you include the file correctly.

  1. Have you downloaded this validator plugin and put the jquery.validate.js file in the Scripts folder?
  2. In the page in which your are using validation: Is this page really using that master page?
  3. In the master page: Is the include of the validation plugin script before the ContentPlaceHolder?
Protron
Well... it was infront of the ContentPlaceHolder ID="MainContent" but not the "TitleContent". Good catch.
Justin