Reputation: 13
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?
Upvotes: 1
Views: 5982
Reputation: 5443
Try this:
<script>$(document).ready(function() {
$('select').change(function() {
$('option[value=' + $(this).val() + ']').attr('disabled', 'disabled');
});
});
Upvotes: 0
Reputation: 4109
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
Upvotes: 2
Reputation: 4109
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.
Upvotes: 0