1

I have a form with set of dropdown fields, each of which has a default value of "0" and available values of 1-3. These are serving as a priority selection, where the user picks their top 3 items. Each select field name is unique and populated in a PHP foreach loop -- I'm just calling it "N" in the example below.

<select class="variable_priority unique required" name="select-N">
    <option value="0"></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

The "0" value is used as the default/preset value for non-prioritized selections. Using the techniques described here, I have successfully modified JQuery Validate to ensure that the user cannot pick 1, 2 or 3 in more than one dropdown by comparing the class "variable_priority":

$('.variable_priority').each(function () {
            if ($(this).val() === value && $(this).val() > 0) {
                timeRepeated++;
            }
        });

However, I still need to validate that the user has indeed entered a value of 1, 2 and 3 in any of the select fields, and has not skipped any. Can anyone give me some guidance on how to do this within the context of JQuery Validate?

Community
  • 1
  • 1
elshaddai
  • 13
  • 1
  • 4
  • 1
    Add up the values, ensure they're not all = 2 – Diodeus - James MacFarlane Oct 21 '11 at 15:17
  • @Diodeus -- Thanks for the suggestion. I'm a JavaScript newbie, so is there a painless way to sum up a string of values based on selector? For example, in the answer below, based on select.variable_priority :selected. And I assume that it should be =6, i.e. 1+2+3. – elshaddai Oct 22 '11 at 12:04

3 Answers3

2

After re-working the requirement on JS Fiddle. http://jsfiddle.net/halfcube/aLvc6/

I think this maybe more closer to your requirement

HTML

<div class="selects">
    <select class="variable_priority unique required" name="select[]" required>
        <option value="0">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select class="variable_priority unique required" name="select[]" required>
        <option value="0">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select class="variable_priority unique required" name="select[]" required>
        <option value="0">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select class="variable_priority unique required" name="select[]" required>
        <option value="0">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
<div class="value">
    0
</div>

CSS

.valid{
    box-shadow:0 0 10px green;
    -webkit-box-shadow:0 0 10px green;
    -moz-box-shadow:0 0 10px green;
}

JavaScript

$(document).ready(function() {

    function checkSelected(val) {
        var ret = false;
        $(".variable_priority").each(function() {
            if ($(this).val() === val) {
                ret = true;
            }
        });
        return ret;
    }

    function totalValue() {
        var total = 0;
        $(".variable_priority:first option[value!=0]").each(function() {
            total = total + parseInt($(this).val(), 10);
        });
        return total;
    }
    function totalSelectedValue(){
        var total = 0;
        $(".variable_priority option:selected").each(function() {
            total = total + parseInt($(this).val(), 10);
        });
        return total;
    }
    $('.value').html(totalValue());
    $('.variable_priority').change(function() {
   //     variable_priority = variable_priority + parseInt($(this).val(), 10);
        $('.value').html(totalSelectedValue() + " out of " + totalValue());

        $('option', this).each(function() {
            if (checkSelected($(this).val()) && $(this).val() !== "0") {
                $('.variable_priority option[value=' + $(this).val() + ']').attr('disabled', true);
            } else {
                $('.variable_priority option[value=' + $(this).val() + ']').removeAttr('disabled');
            }
        });
        if (totalSelectedValue() === totalValue()) {
            $('.selects').removeClass('invalid').addClass('valid');
        } else {
            $('.selects').removeClass('valid').addClass('invalid');
        }
    });
});

Now I must get back to work.

enjoy

kylewelsby
  • 4,031
  • 2
  • 29
  • 35
  • Thank you so much for the work and insight. I love how it's working on the page and how you've integrated the different validations. However, as best as I can tell, because the – elshaddai Oct 26 '11 at 02:09
  • Never mind - I was able to solve this using some binding code [found here](http://stackoverflow.com/questions/1191113/disable-select-form-field-but-still-send-the-value/1191365#1191365): $('form').bind('submit', function() { $(this).find('option').removeAttr('disabled'); }); – elshaddai Oct 26 '11 at 04:43
0

Try this:

<script>$(document).ready(function() {
$('select').change(function() {
    $('option[value=' + $(this).val() + ']').attr('disabled', 'disabled');
});

});

Imtiaz Pabel
  • 5,307
  • 1
  • 19
  • 24
0

I would firstly get the browser to do most of the work, so I would use HTML5 attributes like required disabled and selected for good mesure.

You could try something like this.

<select class="variable_priority unique required" name="select-N" required>
  <option value="0" disabled></option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

This should be enough to allow the browser to show blank as the default value then once the user selects an option they can't select blank again.

For safe mesure I would put a JavaScript side validation binded to the form submit event to ensure the form is valid.

Something like this;

$("form").bind("submit",function(){
  if($("select.variable_priority :selected").length >= $("select.variable_priority").length){
    return true;
  }else{
    return false;
  }
});

This if statement will check the count of selected dropdowns compared to the total number of dropdowns on the page. If they match then the form is valid.

I hope this helps you out.

kylewelsby
  • 4,031
  • 2
  • 29
  • 35
  • Thanks for the thoughts. I'm not able to go to HTML5 quite yet, but I appreciate the insight. As for the validation code, I've tried this: `if( $("select.variable_priority :selected").length == 8 )` In my test case, my select object has 8 options. The script above only returns true if I have length==8. It should be length==3 to validate that three entries have been made, i.e. priority values 1, 2 and 3. I've modified my default option to not return a value (), but somehow they're still being counted against the total :selected. – elshaddai Oct 22 '11 at 11:30
  • I'm getting the thought that your using a ` – kylewelsby Oct 24 '11 at 14:51
  • No - sorry if I wasn't clear. In my test case, I have a set of eight – elshaddai Oct 24 '11 at 19:25
  • Here is the validation used for testing the specific values: ` varsum: function (value, element) { var sumOfValues=0; $(".variable_priority").each(function(){ sumOfValues+=Number($(this).val()); }); if(sumOfValues==6){ return true } else { return false } },` – elshaddai Oct 24 '11 at 19:33
  • Here is the validation used for testing the unique values: ` unique: function (value, element) { var parentForm = $(element).closest('form'); var timeRepeated = 0; $('.variable_priority').each(function () { if ($(this).val() === value && $(this).val() > 0) { timeRepeated++; } }); if (timeRepeated === 1 || timeRepeated === 0) { return true } else { return false } },` – elshaddai Oct 24 '11 at 19:35
  • FWIW, the specific value validation is based on a technique [described here](http://stackoverflow.com/questions/4225121/jquery-validate-sum-of-multiple-input-values). – elshaddai Oct 24 '11 at 19:40