Link to home
Start Free TrialLog in
Avatar of Phil Catterall
Phil Catterall

asked on

Problem with client-side asp.net validator

I'm quite new to this and need some help with a custom asp.net custom validator.

I have a function that adds a validator dynamically to an input. For checkboxes I have written a custom validator.

function AddValidator(controlId, validationMessage, isCheckBox = false) {
    if (typeof (Page_Validators) == 'undefined') return;
    // first remove validator so we do't add more than 1
    removeValidator(controlId);
    // Create new validator
    var newValidator = document.createElement('span');
    newValidator.style.display = "none";
    newValidator.display = "Dynamic";
    newValidator.id = "RequiredFieldValidator" + controlId;
    newValidator.controltovalidate = controlId;
    newValidator.errormessage = "<a href='#" + controlId + "_label' onclick='javascript:scrollToAndFocus(\"" + controlId + "_label\"" + ",\"" + controlId + "\");return false'>" + validationMessage + "is required.</a>";
    newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form
    newValidator.initialvalue = "";
    newValidator.evaluationfunction = (isCheckBox == true) ? CheckBoxCheckedValidator : RequiredFieldValidatorEvaluateIsValid;
    Page_Validators.push(newValidator);
    $("#" + controlId + '_label').parent().removeClass('info');
    $("#" + controlId + '_label').parent().addClass('info required');
}

Open in new window


I then add the validator with this snippet
AddValidator($(this).attr("id"),"Validation Message ",true);

Open in new window


And the custom function is here:
function CheckBoxCheckedValidator()
{
return $(this).prop("checked") ;
}

Open in new window


What this is trying to do is to return false if the checkbox is not checked, and true if it is. But the control that is passed into this custom function is not the checkbox itself, rather it is the control created in the AddValidator function (i.e. RequiredFieldValidatorMyControlName )
So the question is, how do I get to the control itself, to check it's current checked status.
Thanks
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.