Skip to content

Instantly share code, notes, and snippets.

@davidisnotnull
Created February 2, 2022 22:21
Show Gist options
  • Save davidisnotnull/9d4f9f00304c54f40b47cd06501fde8f to your computer and use it in GitHub Desktop.
Save davidisnotnull/9d4f9f00304c54f40b47cd06501fde8f to your computer and use it in GitHub Desktop.
Custom jquery-validation handling for check boxes must equal true
window.onload = () => {
/*
Custom jquery-validation handling for check boxes must equal true
Decorate C# model bool properties with
[Range(typeof(bool), 'true', 'true', ErrorMessage = 'Add your error message')]
to make their corresponding checkbox validate to 'must equal true'
*/
var defaultRangeValidator = $.validator.methods.range;
$.validator.methods.range = function (value, element, param) {
if (element.type === 'checkbox') {
// if it's a checkbox return true if it is checked
return element.checked;
} else {
// otherwise run the default validation function
return defaultRangeValidator.call(this, value, element, param);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment