You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/pages/abide.md
+40
Original file line number
Diff line number
Diff line change
@@ -246,6 +246,46 @@ When the Form Errors cannot be placed next to its field, like in an Input Group,
246
246
</form>
247
247
```
248
248
249
+
## Required Radio & Checkbox
250
+
251
+
If you add `required` to a radio or checkbox input the whole group gets considered as required. This means at least one of the inputs must be checked.
252
+
Checkbox inputs support the additional attribute `data-min-required` what lets you specify how many checkboxes in the group must be checked (default is one).
* Determines whether or a not a checkbox input is valid based on whether or not it is required and checked. Although the function targets a single `<input>`, it validates by checking the `required` and `checked` properties of all checkboxes in its group.
565
+
* @param {String} groupName - A string that specifies the name of a checkbox group
566
+
* @returns {Boolean} Boolean value depends on whether or not at least one checkbox input has been checked (if it's required)
567
+
*/
568
+
validateCheckbox(groupName){
569
+
// If at least one checkbox in the group has the `required` attribute, the group is considered required
570
+
// Per W3C spec, all checkboxes in a group should have `required`, but we're being nice
// For the group to be valid, the minRequired amount of checkboxes have to be checked
595
+
if(checked>=minRequired){
596
+
valid=true;
597
+
}
598
+
};
599
+
600
+
// Skip validation if more than 1 checkbox have to be checked AND if the form hasn't got submitted yet (otherwise it will already show an error during the first fill in)
601
+
if(this.initialized!==true&&minRequired>1){
602
+
returntrue;
603
+
}
604
+
605
+
// Refresh error class for all input
606
+
$group.each((i,e)=>{
607
+
if(!valid){
608
+
this.addErrorClasses($(e));
609
+
}else{
610
+
this.removeErrorClasses($(e));
611
+
}
612
+
});
613
+
614
+
returnvalid;
615
+
}
616
+
498
617
/**
499
618
* Determines if a selected input passes a custom validation function. Multiple validations can be used, if passed to the element with `data-validator="foo bar baz"` in a space separated listed.
<p>This form has a required checkbox with a custom value for <strong>data-min-required</strong> that specifies how many checkboxes must be checked in the group. If you try to submit without checking at least 3 checkboxes, it
75
+
should show an error. When you then pick the required amount, the error should clear and let you submit.</p>
0 commit comments