Created
February 2, 2022 22:21
-
-
Save davidisnotnull/9d4f9f00304c54f40b47cd06501fde8f to your computer and use it in GitHub Desktop.
Custom jquery-validation handling for check boxes must equal true
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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