You can use a regular RequiredValidator for i.e. a textbox but not for a label, so I’ve added a custom validator and use JQuery function to validate it. All works fine but I would like to be able to unit test it.
What changes would I need to do to, to test this function:
function LabelRequired_Validate(sender, args) {
if (sender == $("#ctl00_cphMain_Agent2_Agent_Agent_LabelValidator1")[0]) {
if ($("#ctl00_cphMain_Agent2_Agent_Agent_ValueLabel")[0].innerText.length > 0) {
args.IsValid = true;
} else {
args.IsValid = false;
}
} else {
args.IsValid = true;
}
}
with this QUnit test (or similar):
test("Servicing_Topup_Branch_label_is_valid", function() {
var span = $("span[id$=ValueLabel]");
$(span).text(10 + " characters long");
var args = { IsValid: true, Value: "" };
LabelRequired_Validate($(span), args)
ok(args.IsValid == true, "Validation Passed!");
});
Here is my custom validator:
<asp:CustomValidator ID="LabelValidator1" runat="server" ErrorMessage="<%$Resources:Main,RequiredFieldWarning %>"
Display="Dynamic" ClientValidationFunction="LabelRequired_Validate" EnableViewState="False"
class="alertMsg">
</asp:CustomValidator>
Made a few changes to both the target and test functions. Here’s how it works.
To the target:
And to the test: