Skip to content

Commit

Permalink
Add support for validating checkboxes/radio buttons
Browse files Browse the repository at this point in the history
Fixes #36

This code was pulled from the sample code contributed in #43.

Co-Authored-By: Andy Butland <[email protected]>
  • Loading branch information
haacked and AndyButland committed Jun 6, 2023
1 parent 15e5855 commit 8f54df2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
21 changes: 0 additions & 21 deletions Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,6 @@
<script src="/dist/aspnet-validation.js"></script>
<script>
const service = new aspnetValidation.ValidationService(console);
var customRequired = function (value, element) {
// Handle single and multiple checkboxes/radio buttons.
if (element.type.toLowerCase() === "checkbox" || element.type.toLowerCase() === "radio") {
const allElementsOfThisName = element.form.querySelectorAll("input[name='" + element.name + "']");
for (let i = 0; i < allElementsOfThisName.length; i++) {
if (allElementsOfThisName[i].checked === true) {
return true;
}
}
return false;
}
// Use default required for all other field types.
return Boolean(value);
}
service.addProvider("required", customRequired);
service.bootstrap();
</script>
</body>
Expand Down
13 changes: 13 additions & 0 deletions dist/aspnet-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ var MvcValidationProviders = /** @class */ (function () {
* Validates whether the input has a value.
*/
this.required = function (value, element, params) {
// Handle single and multiple checkboxes/radio buttons.
var elementType = element.type.toLowerCase();
if (elementType === "checkbox" || elementType) {
var allElementsOfThisName = Array.from(element.form.querySelectorAll("input[name='".concat(element.name, "'][type='").concat(elementType, "']")));
for (var _i = 0, allElementsOfThisName_1 = allElementsOfThisName; _i < allElementsOfThisName_1.length; _i++) {
var element_1 = allElementsOfThisName_1[_i];
if (element_1 instanceof HTMLInputElement && element_1.checked === true) {
return true;
}
}
return false;
}
// Default behavior otherwise.
return Boolean(value);
};
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ export class MvcValidationProviders {
* Validates whether the input has a value.
*/
required: ValidationProvider = (value, element, params) => {
// Handle single and multiple checkboxes/radio buttons.
const elementType = element.type.toLowerCase();
if (elementType === "checkbox" || elementType) {
const allElementsOfThisName = Array.from(element.form.querySelectorAll(`input[name='${element.name}'][type='${elementType}']`));
for (let element of allElementsOfThisName) {
if (element instanceof HTMLInputElement && element.checked === true) {
return true;
}
}

return false;
}
// Default behavior otherwise.
return Boolean(value);
}

Expand Down

0 comments on commit 8f54df2

Please sign in to comment.