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

  • SEARCH
  • Home
  • 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 8960831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:44:50+00:00 2026-06-15T15:44:50+00:00

Relates to: ASP.NET MVC 3 client-side validation with parameters I built a custom validation

  • 0

Relates to: ASP.NET MVC 3 client-side validation with parameters

I built a custom validation attribute allowing me to check relative dates (ex. model value less than or equal to Today).

The validator correctly implements the GetClientValidationRules method and the HTML5 emitted in the view looks correct to me:

<div class="editor-label">
    <label for="Date">Date</label>
</div>
<div class="editor-field">
    <input class="text-box single-line" data-val="true" data-val-date="The field Date must be a date." data-val-relativedate="Date must be less than or equal to 12/04/2012" data-val-relativedate-referencedate="12/04/2012 00:00:00" data-val-relativedate-relativityoperator="lessThanOrEqual" data-val-required="The Date field is required." id="Date" name="Date" type="date" value="" />
    <span class="field-validation-valid" data-valmsg-for="Date" data-valmsg-replace="true"></span>
</div>

The next step was to define a custom jQuery Unobtrusive Validator adapter and method:

// Validation Method: Relative Date
// Note: method name value does NOT need to match the HTML5 metadata built by the server model annotation attribute.
$.validator.addMethod("validateRelativeDate", function (value, element, params) {
    // "element" is the actual HTML element we are validating, and is unneeded here.
    // "value" is the value of the HTML element
    var toValidate = DateUtilities.convert(value);
    // "params" is a JSON collection of those values provided by the adapter.
    var referenceDate = params["referencedate"];
    var relativityOperator = params["relativityoperator"];

    if (relativityOperator != "lessThan" && relativityOperator != "lessThanOrEqual" || relativityOperator != "greaterThan" && relativityOperator != "greaterThanOrEqual")
        return false; // "Invalid relativity operator (ex. '<', '<=', '>', or '>=').");

    var toReference = referenceDate == null ? new Date() : referenceDate;

    var relativity = DateUtilities.compare(toValidate, toReference);

    switch(relativityOperator) {
        case "lessThan":
            if (relativity >= 0) return false; //"Date must be lesser than " + toReference);
        break;
        case "lessThanOrEqual":
            if (relativity > 0) return false; //"Date must be less than or equal to " + toReference);
        break;
        case "greaterThan":
            if (relativity <= 0) return false; //"Date must be greater than " + toReference);
        break;
        case "greaterThanOrEqual":
            if (relativity < 0) return false; //"Date must be greater than or equal to " + toReference);
        break;
    }

    return true;

});

// note: adapter name must match HTML metadata built by the server model annotation attribute
$.validator.unobtrusive.adapters.add("relativedate", ["referencedate", "relativityoperator"], function (options) {
    options.rules["relativedate"] = options.params;
    options.messages["relativedate"] = options.message;
});

Symptom

The date validation is broken. Required seems to work fine. But whenever I type any value in this field the Date type validator triggers and produces the message “The field Date must be a date”. This occurs for ANY date value entered.

Question

What is wrong with my validation rule that it (1) does not work, and (2) sabotages the Date type validation?

  • 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-06-15T15:44:52+00:00Added an answer on June 15, 2026 at 3:44 pm

    As with most debugging experiences, the symptom had nothing to do with what was going on. Both the Required and Date Type validations were succeeding. The failure was caused in the adapter.

    Explanation

    Using browser script debuggers I found it failing on this line:

    // jQuery.validate.js, line 530
    result = $.validator.methods[method].call( this, val, element, rule.parameters );
    

    This is where jQuery loops through the rules and their associated methods for validation.

    method then was equal to "relativedate" (the metadata value spit out in HTML5) and thus the call property was throwing an exception. I named my method differently $.validator.addMethod("validateRelativeDate", /*...*/) and the documentation said it did not have to match this metadata value… so why was this happening?

    It’s the adapter. I knew the adapter name must be the same as the HTML5 metadata, which is the tight coupling between the two – thus "relativedate" seen below.

    // note: adapter name must match HTML metadata built by the server model annotation attribute
    $.validator.unobtrusive.adapters.add(“relativedate”, [“referencedate”, “relativityoperator”], function (options) {
    options.rules[“relativedate”] = options.params;
    options.messages[“relativedate”] = options.message;
    });

    The two lines adding array values for rules and messages were the culprit. Apparenly these values need to be the name of the method used for validation, not the name of the adapter/metadata.

    Solution

    Changing these two lines to match the validation method name, solved everything:

    options.rules["validateRelativeDate"] = options.params;
    options.messages["validateRelativeDate"] = options.message;
    

    Excuses, excuses…

    All of the examples I researched and even the one I referenced, all of these values are equal so this detail was never discussed. But ultimately it means I was a bit of a lackwit programmer not to realize that this is the only way it “adapts” the HTML5 crap to the validation method.

    Hopefully this helps someone or saves them time.

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

Sidebar

Related Questions

Greetings, The question relates to ASP.NET MVC I am creating some divs dynamically using
I am trying to fix a design issue related to ASP.NET MVC 3 validation
This question relates to ASP.NET MVC 2 RC (December drop). The basic problem is
ASP.NET MVC has a bunch of custom item templates to create controllers, views, etc.
Related questions: ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout
I have developed a system for a client using ASP.Net MVC 3 and Entity
With ASP.NET MVC controllers, the controller itself responds with requests from the client to
This is actually related with ASP.NET MVC. When a form is submitted, model validation
This question is related to my ASP.NET MVC 2 development, but it could apply
I have an ASP.NET MVC view and related model. How can I fill its

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.