Skip to content

Commit

Permalink
Merge pull request #44 from haacked/haacked/36-validate-radio-buttons
Browse files Browse the repository at this point in the history
Add support for validating checkboxes/radio buttons
  • Loading branch information
haacked authored Jun 7, 2023
2 parents 15e5855 + b2668aa commit d6d4aff
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 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.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aspnet-client-validation",
"version": "0.8.7",
"version": "0.8.8",
"description": "Enables ASP.NET MVC client-side validation, without jQuery!",
"main": "dist/aspnet-validation.js",
"style": "dist/aspnet-validation.css",
Expand Down
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 d6d4aff

Please sign in to comment.