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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:56:19+00:00 2026-05-31T14:56:19+00:00

I am trying to validate if a check box is checked on the client

  • 0

I am trying to validate if a check box is checked on the client using FluentValidation. I can’t figure it our for the life of me.

Can it be done using unobtrusive 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-05-31T14:56:21+00:00Added an answer on May 31, 2026 at 2:56 pm

    Let’s assume that you have the following model:

    [Validator(typeof(MyViewModelValidator))]
    public class MyViewModel
    {
        public bool IsChecked { get; set; }
    }
    

    with the following validator:

    public class MyViewModelValidator : AbstractValidator<MyViewModel>
    {
        public MyViewModelValidator()
        {
            RuleFor(x => x.IsChecked).Equal(true).WithMessage("Please check this checkbox");
        }
    }
    

    and a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    with a corresponding view:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.IsChecked)
        @Html.CheckBoxFor(x => x.IsChecked)
        @Html.ValidationMessageFor(x => x.IsChecked)
        <button type="submit">OK</button>
    }
    

    and in Global.asax you have registered the fluent validation model validator provider:

    FluentValidationModelValidatorProvider.Configure();
    

    So far we have server side validation up and running fine. That’s good. That’s always the first part that we must setup. I have seen people focusing too much on doing client side validation that they forget doing server side validation and when you disable javascript (or even worse if you stumble upon a user with bad intentions), well, bad things happen.
    So far we are confident because we know that even if something gets screwed up on the client our domain is protected with server side validation.


    So let’s now take care for the client validation. Out of the box FluentValidation.NET supports automatic client validation for the EqualTo validator but when comparing against another property value which is the equivalent of the [Compare] data annotation.

    But in our case we are comparing against a fixed value. So we don’t get client side vaildation out of the box. And when we don’t get something out of the box, we need to put it in the box.

    So we start by defining a custom FluentValidationPropertyValidator:

    public class EqualToValueFluentValidationPropertyValidator : FluentValidationPropertyValidator
    {
        public EqualToValueFluentValidationPropertyValidator(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 = (EqualValidator)Validator;
    
            var errorMessage = new MessageFormatter()
                .AppendPropertyName(Rule.GetDisplayName())
                .AppendArgument("ValueToCompare", validator.ValueToCompare)
                .BuildMessage(validator.ErrorMessageSource.GetString());
    
            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = errorMessage;
            rule.ValidationType = "equaltovalue";
            rule.ValidationParameters["valuetocompare"] = validator.ValueToCompare;
            yield return rule;
        }
    }
    

    that we are going to register in Application_Start:

    FluentValidationModelValidatorProvider.Configure(provider =>
    {
        provider.AddImplicitRequiredValidator = false;
        provider.Add(typeof(EqualValidator), (metadata, context, description, validator) => new EqualToValueFluentValidationPropertyValidator(metadata, context, description, validator));
    });
    

    So far we have associated our custom FluentValidationPropertyValidator with the EqualValidator.

    The last part is to write a custom adapter:

    (function ($) {
        $.validator.unobtrusive.adapters.add('equaltovalue', ['valuetocompare'], function (options) {
            options.rules['equaltovalue'] = options.params;
            if (options.message != null) {
                options.messages['equaltovalue'] = options.message;
            }
        });
    
        $.validator.addMethod('equaltovalue', function (value, element, params) {
            if ($(element).is(':checkbox')) {
                if ($(element).is(':checked')) {
                    return value.toLowerCase() === 'true';
                } else {
                    return value.toLowerCase() === 'false';
                }
            }
            return params.valuetocompare.toLowerCase() === value.toLowerCase();
        });
    })(jQuery);    
    

    And that’s pretty much it. All that’s left is to include the client scripts:

    <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>
    <script src="@Url.Content("~/Scripts/customadapter.js")" type="text/javascript"></script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate a model containing other objects with validation rules using the
Can't seem to get checkbox to be validate on client-side using asp.net mvc 2.
I'm trying to validate the following XML but I'm unable to, can you please
I was trying to validate an XML signature. The validation according to this tutorial
I’m trying to validate input by using egrep and regex.Here is the line from
I'm trying to validate a input field with the JQuery validation plugin. The numeric
I'm trying to check username and password by using a For Each Statement. If
I am trying to validate an html form using javascript the code is bellow
I am trying to have a checkboxes validation using Javascript, and i am really
I'm trying to validate a form using the validate plugin for jquery. I want

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.