Saturday, 27 February 2021

MVC - Custom Validation - Check in checkbox is selected (Checked) in JavaScript

 Watch this example on YouTube


To fix it replace 

 $.validator.addMethod('validatecheckbox', function (value, element, params) {
    var isValid = true;
    if ($(params)[0].fields[0][0].val() == true) {
        if (($(element).val() == '' || $(element).val() == null)) {
            isValid = false;
        }
    }
    return isValid;
});

with

$.validator.addMethod('validatecheckbox', function (value, element, params) {
    var isValid = true;
    if ($(params)[0].fields[0][0].checked == true) {
        if (($(element).val() == '' || $(element).val() == null)) {
            isValid = false;
        }
    }
    return isValid;
});

 

No comments:

Post a Comment