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 7606613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:27:16+00:00 2026-05-31T00:27:16+00:00

I have the following rules the 1st does work using unobtrusive, client side validation,

  • 0

I have the following rules

the 1st does work using unobtrusive, client side validation, the second does not

any ideas why?

RuleFor(x => x.StartDate)
    .LessThanOrEqualTo(x => x.EndDate.Value)
    .WithLocalizedMessage(() => CommonRes.Less_Than_Or_Equal_To, filters => CommonRes.Start_Date, filters => CommonRes.End_Date);

RuleFor(x => x.StartDate)
    .GreaterThanOrEqualTo(x => x.AbsoluteStartDate)
    .LessThanOrEqualTo(x => x.AbsoluteEndDate)
    .WithLocalizedMessage(() => CommonRes.Between, filters => CommonRes.Start_Date, filters => filters.AbsoluteStartDate, filters => filters.AbsoluteEndDate);
  • 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-31T00:27:17+00:00Added an answer on May 31, 2026 at 12:27 am

    Neither of the LessThanOrEqualTo or GreaterThanOrEqualTo rules are supported by client side validation as explained in the documentation.

    This means that if you want to have client side validation for them you will need to write a custom FluentValidationPropertyValidator and implement the GetClientValidationRules method which will allow you to register a custom adapter and implement the client side validation logic for it in javascript.

    If you are interested on how this could be achieved just ping me and I will provide an example.


    Update

    As request, I will try to show an example of how one could implement custom client side validation for the LessThanOrEqualTo rule. It is only a particular case with non-nullable dates. Writing such custom client side validator for all the possible case is of course possible but it will require significantly more efforts.

    So we start with a view model and a corresponding validator:

    [Validator(typeof(MyViewModelValidator))]
    public class MyViewModel
    {
        [Display(Name = "Start date")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime StartDate { get; set; }
    
        public DateTime DateToCompareAgainst { get; set; }
    }
    
    public class MyViewModelValidator : AbstractValidator<MyViewModel>
    {
        public MyViewModelValidator()
        {
            RuleFor(x => x.StartDate)
                .LessThanOrEqualTo(x => x.DateToCompareAgainst)
                .WithMessage("Invalid start date");
        }
    }
    

    Then a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                StartDate = DateTime.Now.AddDays(2),
                DateToCompareAgainst = DateTime.Now
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    and a view:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.Hidden("DateToCompareAgainst", Model.DateToCompareAgainst.ToString("yyyy-MM-dd"))
    
        @Html.LabelFor(x => x.StartDate)
        @Html.EditorFor(x => x.StartDate)
        @Html.ValidationMessageFor(x => x.StartDate)
        <button type="submit">OK</button>
    }
    

    All this is standard stuff so far. It will work but without client validation.

    The first step is to write the FluentValidationPropertyValidator:

    public class LessThanOrEqualToFluentValidationPropertyValidator : FluentValidationPropertyValidator
    {
        public LessThanOrEqualToFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
            : base(metadata, controllerContext, rule, validator)
        {
        }
    
        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            if (!this.ShouldGenerateClientSideRules())
            {
                yield break;
            }
    
            var validator = Validator as LessThanOrEqualValidator;
    
            var errorMessage = new MessageFormatter()
                .AppendPropertyName(this.Rule.GetDisplayName())
                .BuildMessage(validator.ErrorMessageSource.GetString());
    
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = errorMessage,
                ValidationType = "lessthanorequaldate"
            };
            rule.ValidationParameters["other"] = CompareAttribute.FormatPropertyForClientValidation(validator.MemberToCompare.Name);
            yield return rule;
        }
    }
    

    which will be registered in Application_Start when configuring our FluentValidation provider:

    FluentValidationModelValidatorProvider.Configure(x =>
    {
        x.Add(typeof(LessThanOrEqualValidator), (metadata, context, rule, validator) => new LessThanOrEqualToFluentValidationPropertyValidator(metadata, context, rule, validator));
    });
    

    And the last bit is the custom adapter on the client. So we add of course the 2 scripts to our page in order to enable unobtrusive client side validation:

    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    

    and the custom adapter:

    (function ($) {
        $.validator.unobtrusive.adapters.add('lessthanorequaldate', ['other'], function (options) {
            var getModelPrefix = function (fieldName) {
                return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
            };
    
            var appendModelPrefix = function (value, prefix) {
                if (value.indexOf("*.") === 0) {
                    value = value.replace("*.", prefix);
                }
                return value;
            }
    
            var prefix = getModelPrefix(options.element.name),
                other = options.params.other,
                fullOtherName = appendModelPrefix(other, prefix),
                element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
    
            options.rules['lessthanorequaldate'] = element;
            if (options.message != null) {
                options.messages['lessthanorequaldate'] = options.message;
            }
        });
    
        $.validator.addMethod('lessthanorequaldate', function (value, element, params) {
            var parseDate = function (date) {
                var m = date.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/);
                return m ? new Date(parseInt(m[1]), parseInt(m[2]) - 1, parseInt(m[3])) : null;
            };
    
            var date = parseDate(value);
            var dateToCompareAgainst = parseDate($(params).val());
    
            if (isNaN(date.getTime()) || isNaN(dateToCompareAgainst.getTime())) {
                return false;
            }
    
            return date <= dateToCompareAgainst;
        });
    
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to enable validation work: $(document).ready(function() { $(#eventForm).validate({ rules: {
I'm having trouble getting the Jquery Validation to work with the following rules. I
I am using jQuery Validation Plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) I have wrote following rule jQuery.validator.addMethod(valueNotEquals, function(value,
On my client registration form I have the following rules // Define the div
I have the following validation rules to my form: function ValidadeRegistration() { $('#RegistrationForm').validate({ rules:
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have defined a fieldset in HTML, and applied the following (simple) CSS rules
I'm using GNU Make 3.81, and I have the following rule in my Makefile:
I have the following rewrite rules <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME}
I have the following rewrite rules: #remove the www. RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC] RewriteRule

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.