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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:22:15+00:00 2026-06-13T06:22:15+00:00

I’ve read every other jQuery validation question I could find and nothing is helping

  • 0

I’ve read every other jQuery validation question I could find and nothing is helping me. I’m trying to do something that should be so simple but unobtrusive validation makes me want to punch a hole in my all four of my monitors (jealous?)

I have one field in the form of a select, and another as a date picker. When the select takes on a certain value, the date becomes required. For all other select values, the date is optional. When the page first loads, the date is optional – that is, there is no Required attribute in the model metadata.

Here is my script block along with holes for what I need help with:

<script type="text/javascript">
    function DateRequirement() {
        if ($("#StatusID").val() == 602) {
            $("#DateOfFirstCoverage").attr("data-val-required", "A date is required for current members.");
            // some kind of re-validating here?
        }
        else if ($("#DateOfFirstCoverage").attr("data-val-required") !== undefined) {
            $("#DateOfFirstCoverage").rules("remove", "required");
            // some kind of re-validating here?
        }
    }

    $("#StatusID").change(DateRequirement);
    $(document).ready(DateRequirement);
</script>

Just FYI I have tried other ways of adding the requirement, such as using .rules("add", { required: true }) and it still has problems. Here’s my list of problems:

  1. With the .rules method of adding required, I get the error of settings being undefined. Plenty of SO answers say you need to re-validate the form first, which I tried, except that although it gets rid of the error, the field still doesn’t become required.
  2. The way I have the code now, shown above, successfully makes the field required, but ONLY if it is set during document ready. If I change the select box after the load, it will never become required.
  3. The weirdest problem of all, no matter what I try, anything that gives any hint of success ruins the rest of the validation on the page. I have a bunch of other required fields. If my DateOfFirstCoverage field is successfully made to be required, and I fill it out but delete a different required field, the page happily submits without validation complaining about the other fields. This happens, for example, if part of my snippet looks like this:

    if ($("#StatusID").val() == 602) {
        $("form").validate();
        $("#DateOfFirstCoverage").rules("add", { required: true });
    }
    

So, what the heck is going on? What is the “correct” way to accomplish what I want without messing with the rest of my rules?

  • 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-13T06:22:16+00:00Added an answer on June 13, 2026 at 6:22 am

    Figured this one out. The correct answer was available in one of the other posts – this one – but I had to figure out how to use it. The final script block is this:

    <script type="text/javascript">
        function DateRequirement() {
            $("form").removeData("validator").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse("form");
    
            if ($("#StatusID").val() == 602) {
                $("#DateOfFirstCoverage").rules("add", { required: true, messages: { required: "A date is required for current members." } });
            }
            else {
                $("#DateOfFirstCoverage").rules("remove", "required");
                $("form").validate().element("#DateOfFirstCoverage");
                console.log("reset");
            }
        }
    
        $("#StatusID").change(DateRequirement);
        $(document).ready(DateRequirement);
    </script>
    

    Removing the validators at the top makes sure that unobtrusive validation doesn’t bail out early because a validator is already hooked up. That solves my weird issue of why the other fields weren’t validating. Then by asking unobtrusive to parse, there always will be a validator, so I can use the normal rules add method for the required rule.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
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

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.