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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:56:54+00:00 2026-05-20T04:56:54+00:00

I’m going to lay out the code to explain the whole situation then pose

  • 0

I’m going to lay out the code to explain the whole situation then pose the description of the problem:

In my View Model I have a boolean property to track whether the user has accepted terms:

 [MustBeTrue(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "MustAccept")]
 public bool HasAuthorizedBanking { get; set; }

As you can see I’ve created a custom validation attribute to handle this called MustBeTrue to handle the Checkbox, since [Required] is currently not working for client-side validation on Checkboxes in MVC 3

public class MustBeTrueAttribute : ValidationAttribute, IClientValidatable
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if ((bool)value)
            return ValidationResult.Success;

        return new ValidationResult(String.Format(ErrorMessageString,validationContext.DisplayName));
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "truerequired"
        };

        yield return rule;
    }
}

Then I add a CheckBoxFor with a ValidationMessage to my View:

@Html.CheckBoxFor(model => model.HasAuthorizedBanking)
@Html.ValidationMessageFor(model => model.HasAuthorizedBanking, "", new { @class = "validationtext" })  

In order to implement this client-side I created a jQuery validator method, and added an unobtrusive adapter:

// Checkbox Validation
jQuery.validator.addMethod("checkrequired", function (value, element) {
    var checked = false;
    checked = $(element).is(':checked');
    return checked;
}, '');

jQuery.validator.unobtrusive.adapters.addBool("truerequired", "checkrequired");

In my view all the steps in the signup process are on one page, the elements are hidden and shown via jQuery and validated using jQuery Validation. When the ‘Next’ button is clicked each input element on the page is triggered for validation:

 var validator = $("#WizardForm").validate(); // obtain validator
    var anyError = false;
    $step.find("input").each(function () {
        if (!validator.element(this)) { // validate every input element inside this step
            anyError = true;
        }

    });

    if (anyError)
        return false;

Notes:

  • There is only one property in my model with the MustBeTrue attribute, and there is only one CheckBoxFor & matching ValidationMessageFor on the entire page.
  • In order to track when this method is being called I simply put an alert(checked); inside the jQuery Validator method ‘checkrequired’.

The problem: When the checkbox is checked/unchecked the ‘checkrequired’ method is fired once. However, when the ‘Next’ button is clicked and we set off to validate all the input elements, it is fired twice whether or not the checkbox is checked. Interestingly, if it is checked, the first validation returns true and the second returns false (this second false return is my main problem – the page will not validate and it will not allow you to continue to the next step). Additionally, when it is checked and Next is clicked – the ValidationMessageFor message disappears as though it is valid.

Edit: I have another Custom Attribute for validating Age in a jQuery DatePicker textbox, and while it is implemented this exact same way – it only fires once under the same conditions.

  • 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-20T04:56:54+00:00Added an answer on May 20, 2026 at 4:56 am

    It turns out the issue is that a second input element with the same name as the checkbox was being generated by Html.CheckBoxFor, as described here: http://forums.asp.net/t/1314753.aspx

    <input data-val="true" data-val-required="The HasAuthorizedBanking field is required." data-val-truerequired="You must accept to continue." id="bankingtermscheckbox" name="HasAuthorizedBanking" type="checkbox" value="true" />
    <input name="HasAuthorizedBanking" type="hidden" value="false" />
    

    The fix was to change the jQuery selector to only look at input elements whose type is not hidden:

    $step.find(':input:not(:hidden)').each(function () { // Select all input elements except those that are hidden
            if (!validator.element(this)) { // validate every input element inside this step
                anyError = true;
            }
    
        });
    
        if (anyError)
            return false; // exit if any error found
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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 some data like this: 1 2 3 4 5 9 2 6
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

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.