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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:36:30+00:00 2026-06-08T09:36:30+00:00

I have a rule that says for each Stage the total quantity selected must

  • 0

I have a rule that says for each Stage the total quantity selected must equal the number of passengers on a booking. Each Stage is defined in a DIV like in this example:-

HTML

<div id="Stage_1">
    <select data-stage="1" id="Quantity1" name="Items[1].Quantity">...</select>
    <select data-stage="1" id="Quantity2" name="Items[2].Quantity">...</select>
    <select data-stage="1" id="Quantity3" name="Items[3].Quantity">...</select>
</div>
<div id="Stage_2">
    <select data-stage="2" id="Quantity4" name="Items[4].Quantity">...</select>
    <select data-stage="2" id="Quantity5" name="Items[5].Quantity">...</select>
</div>

So for example if I have a booking with 4 passengers in the above sample the sum value of Quantities 1-3 must equal 4 and also the sum value of the Quantities 4-5 must also equal 4.

I then use the jQuery validation plugin to validate…

Script

function ValidateStages(value, element, options) {
    var stage = options[0];

    var valid = true;
    var arrVS = new Array(0);

    jQuery('div[id*="Stage_' + stage + '"] select[id*="Quantity"]').each(function () {
        var e = jQuery(this);
        if (arrVS[e.attr('data-stage')] == undefined) {
            arrVS[e.attr('data-stage')] = 0;
        }
        arrVS[e.attr('data-stage')] += parseInt(e.val());
    });

    for (key in arrVS) {
        if(arrVS[key] != MaxPassengers) {
            valid = false;
        }
    }

    return valid;
}

jQuery(document).ready(function () {
    jQuery.validator.addMethod("validate_stages", ValidateStages, "Stage {0} error ");

    jQuery('form:first').validate({
        errorContainer: "#ValidationSummary",
        errorLabelContainer: "#ValidationSummary",
        rules: {
            "Items[1].Quantity": { validate_stages: [1] } ,
            "Items[2].Quantity": { validate_stages: [1] } ,
            "Items[3].Quantity": { validate_stages: [1] } ,
            "Items[4].Quantity": { validate_stages: [2] } ,
            "Items[5].Quantity": { validate_stages: [2] } 
        },
    });
});

Questions

I have the following issues:-

  1. The error message is displayed for each dropdown on the page. So for example if says “stage 1 error stage 1 error stage 1 error stage 2 error stage 2 error“. If Stage 1 and Stage 2 are invalid it should just display “stage 1 error stage 2 error“

  2. As you can see the rules are hard coded. I did try to create a function (see below) that creates the rules on the fly but I don’t know how to apply it. If I try and do rules: { GetRules() } then I just get an error saying “expected :”

    var GetRules = function () {
        var rules = '';
        jQuery('div[id*="Stage_"]').each(function (i) {
            jQuery('#' + jQuery(this).attr('id') + ' select[id*="Quantity_"]').each(function (j) {
                var name = jQuery(this).attr('name');
                rules = rules + '"' + name + '": { validate_stages: [' + (i + 1) + '] }, ';
            });
        });
    
        if (rules.length > 2) {
            rules = rules.slice(0, -2);
        }
        return 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-08T09:36:32+00:00Added an answer on June 8, 2026 at 9:36 am

    Figured it out myself. I needed to group the controls on the validator. Once this was done I was able to display each stages error in a separate container. The groups and rules are created dynamically as shown below…

    var validation =
    {
        errorContainer: "#ValidationSummary",
        errorLabelContainer: "#ValidationSummary",
        groups: {}, rules: {}, messages: {}
    };
    
    jQuery('div[id*="Stage_"]').each(function (i) {
        jQuery('#' + jQuery(this).attr('id') + ' select[id*="Quantity_"]').each(function (j) {
            var name = jQuery(this).attr('name');
            var Rule = name;
            validation.rules[Rule] = { validate_stages: [i + 1] };
        });
    });
    
    jQuery('div[id*="Stage_"]').each(function (i) {
        var group_ctrls = ''
        jQuery('#' + jQuery(this).attr('id') + ' select[id*="Quantity_"]').each(function (j) {
            var name = jQuery(this).attr('name');
            group_ctrls = group_ctrls + name + ' ';
        });
        validation.groups['group_' + i] = group_ctrls;
    });
    
    jQuery('form:first').validate(validation);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across this rule in YSlow for performance improvement that says that
I have a custom validation rule on a model that says the user can't
In my ANTLR grammar I have a rule that says a query can be
I think this may be relatively straight forward. I have a rewrite rule that
I have the following rewrite rule that I need to alter in order to
I have a mod_rewrite rule that will send any subdomain requests to a php
I have a rule, that gets up to three parts, separated by a /
With at-rules, it's possible to have a ruleset for elements during that at-rule event
I have the a rule with an action defined as follows: metric_expr : metric=NAME
I have this rule which says use this CSS rule ONLY FOR PRINTING. @media

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.