1

I am loading a form onto a page via jquery ajax. Once loaded I was hoping that <%Html.EnableClientValidation(); %> would work as it normally does. It is not, and I am guessing that this is because the form has been added to the DOM after it was initially set, and the client-side validation scripts are not wired to hanlde "live" content.

Is this assumption correct? Is there a work-around?

I am using the following main scripts to handle client-side validation....

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

Note that I am not using...

<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>

Although the inclusion of this script seems to make no difference.

Also note that I am placing the <%Html.EnableClientValidation(); %> above my form, and so that directive is loaded along with the form via the ajax call.

1
  • One thing that has helped is that I changed the form that was loading to use the <% using (Html.BeginForm(...)}%> as opposed to just manually typing the <form> tag. I found this hint on this site blogs.teamb.com/craigstuntz/2010/09/09/38636. This has made it so that the elements of the form have the proper classes inserted. But, the script that is supposed to be inserted below the 'close form tag' is still missing. Commented Dec 30, 2011 at 15:26

1 Answer 1

1

Is this assumption correct?

Yes, this assumption is correct.

Is there a work-around?

Yes, there is. You may take a look at the following blog post which illustrates the how you could proceed to force client side validation for dynamically loaded contents.

Sign up to request clarification or add additional context in comments.

1 Comment

I read through the blog post, and unless I am missing something, the crux of the solution is to check for validation errors on the submit action of the form with this line of code...if (!Sys.Mvc.FormContext.getValidationForForm(this).validate('submit').length) {...} This does prevent the form from being submitted with errors, but it does not highlight errors as the users moves between fields, and it does not indicate to the user where the error in the form lies. Did I miss something? Also, see my comment above...why does the script for the validation not load below the form?

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.