开发者

How do I use JQuery Validation in ASP.NET MVC?

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?


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜