Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968556
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:51:26+00:00 2026-05-20T03:51:26+00:00

I’m using jQuery Templates for a bunch of a site I’m working on, and

  • 0

I’m using jQuery Templates for a bunch of a site I’m working on, and have reached a point where I need to decide how to go about implementing validation, clientside. Serverside is an Asp.Net Mvc Restful service that deals out various models. The models are decorated with DataAnnotations to describe the validation. Below is an example (note that we’re using a resource file for the error copy):

    [Required(
        ErrorMessageResourceType = typeof(Validation),
        ErrorMessageResourceName = "HomeAddressRequired")]
    [StringLength(250,
        ErrorMessageResourceType = typeof(Validation),
        ErrorMessageResourceName = "HomeAddressStringLength")]
    public string Address { get; set; }

Previously, I just used Razor (or the old Asp.Net view engine until we switched to Mvc 3), which handled generating all of the clientside decoration necessary for jquery.validate.js to hook into. Now that I’m using jQuery Templates, this is not quite as viable.

I see three options here:

  1. Modify the templates to manually include the validation and error copy. This one sucks because I’ll be forced to maintain client and serverside validation separately; it kinda kills the whole idea of DataAnnotations.

  2. Leverage Razor and Mvc 3’s unobtrusive javascript approach to help me render the templates. Better, since it reproduces the validation for me, but at a performance hit. Also means I’m using a serverside template engine…to render templates. I can minimize the performance hit through output caching.

  3. Rely solely on remote validation that returns model states for my models and write my own adapter for jQuery Validate, to handle routing errors to the proper element. This one gets around having to use Razor, but kills pure clientside validation and means I have to write a complicated plugin.

Ideally, when we deploy this thing I should be able to serve the templates to the client as a static file without any direct backend dependency.

So, to my question: Leveraging jQuery Templates clientside and DataAnnotations on my models serverside, what is the most effective means of handling validation without repeating myself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:51:27+00:00Added an answer on May 20, 2026 at 3:51 am

    I’ll go with 2)

    The error templates can sit on the client side. The server side code that is not performance friendly is the code that sets up the rules for jQuery.validation. But that’s really cannot be avoided as your rules are defined in your .NET models.

    I have a similar problem today but with styling, I don’t quite like how jQuery.validation makes a label for you. I want to override the output with jQuery templates. I also want my output to be located in another cell of a table.

    Apparently, you can override validator.prototype.showLabel function in the plugin. Now you have full control of where to display and what to display if something fails.

    Here’s an example:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.js"></script>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js"></script>
    
    
        <script type="text/javascript">
            $.extend($.validator.prototype, {
                showLabel: function (element, message) {
                    $(element).siblings('.validationLabel').remove();
                    $('#tmplValidationLabel').tmpl({ Message: message }).insertAfter($(element));
                }
            });
        </script>
    </head>
    <body>
    
    
    <form>
        <div><input type="text" name="Email" /></div>
        <div><input type="text" name="SomeName" /></div>
        <input type="submit" value="Submit" />
    </form>
    
    
    
    <script type="x-jquery-tmpl" id="tmplValidationLabel">
    <span class="validationLabel">
        <b>${Message}</b> <span> :( </span>
    </span>
    </script>
    
    <script type="text/javascript">
        $(function () {
    
            $('form').validate({
                rules: {
                    Email: { required: true, email: true },
                    SomeName: "required"
                },
                messages: {
                    Email: { required: "Enter Email", email: "Not an email" },
                    SomeName: "Enter something man!"
                }
            });
        });
    </script>
    </body>
    </html>
    

    Hope it helps,

    Chi

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.