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
  • Are you including jQuery later in the page possibly? Commented Sep 13, 2010 at 22:07

2 Answers 2

1

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?
Sign up to request clarification or add additional context in comments.

1 Comment

Well... it was infront of the ContentPlaceHolder ID="MainContent" but not the "TitleContent". Good catch.
0

In my case, the jQuery.validator is null or not an object error was caused by including the jquery.js library twice in the same page and using a different version of jquery.js in each case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.