2

I am using JQuery for all of my client side validation and Asp.net validator controls for all of my server side validation. I am using an errorlabelcontainer to store the client side validations in a summary at the top of the page, which is the requirement. All works well. My problem is, I want to display the server side asp.net errors in the same errorlabelcontainer OR display all of the client side errors in the validation summary. Either way, Both errors need to be in the same place/div. Any ideas on how to do this? I thought of maybe using the asp.net validation summary as the errorcontainer in JQuery, but I cannot find the summary. This is what I have right now.

    $("#aspnetForm").validate(
          {
              onkeyup: false,
              errorLabelContainer: $("ul", $("#FormErrors")),
              wrapper: "li"
         });

If you need to see more code, or for me to clarify something please let me know.

2 Answers2

1

I use xVal to combine errors on the server side with errors on the client side. Typically, what I do is issue an AJAX call (it can be a .ajax call in jQuery or you could use the jQuery form plugin) which returns the error information in JSON and then I make the call on the validator for jQuery validation to populate the error fields.

This is the approach you are generally going to have to take. Otherwise, you are forced to embed Javascript literals in your code when you render the page which the jQuery validation module would then use to populate the appropriate labels. Having that code in your page is a little messy, and the AJAX call returning JSON helps address separation-of-concerns issues.

I've written about how to use jQuery validation with xVal here, which migh be of some help:

How to use the jQuery Validation plugin with metadata, jQuery Forms and xVal together?

Community
  • 1
  • 1
casperOne
  • 73,706
  • 19
  • 184
  • 253
0

Server-side validation needs to work even when a user has disabled their java-script. The ajax approach will not work in that scenario.

Nik
  • 283
  • 2
  • 6