An ASP.NET custom validator allows you to perform ( not surprisingly ) custom validation on user input. In theory, you can specify both a client and a server side function to call.
I have three checkboxes, they are not generated by a checkboxlist, because I want more control over their layout, AND I want to attach some js events on specific options. So, I created a custom validator like this:
<asp:customvalidator runat="server" id="selectOne" controltovalidate="address1" clientvalidationfunction="IsOneSelected" onservervalidate="CheckSelection" errormessage="*" text="*" display="Static">
Note that address1 is not one of my checkboxes, the custom validator will not allow me to validate a checkbox. I've tried removing this property, because some blogs I've read, suggest this. Then I write a validator method like this:
function IsOneSelected(source, args)
var valid = false;
var demo = document.getElementById(demoCD);
var Trial = document.getElementById(trial);
var Webinar = document.getElementById(webinar);
if (Webinar.checked) valid = true;
if (Trial.checked) valid = true;
if (demo.checked) valid = true;
args.isValid = valid;
The alert is obviously a temporary measure. Apparently, the js editor has support for enforced bracing standards, and it defaults to K&R braces, so I kept getting this:
It turns out that the client side event only fires if the control you are validating has been changed. So, in theory if I hook up the control to a textbox that needs to be filled, that textboxes validator will help out if it's not, otherwise, this one will fire and I'll get my validation on the client side, right ? Well, I get my alert, but even if I hard code the validator to ALWAYS fail, the postback always occurs, my validator never stops a postback.
Now for the fun bit. MS apparently respond to complaint about this by pointing out that validation should always occur on the server, so even if the client side fire fails, there should always be a server side that fires. I agree with this, although in our case, there's nothing malicious anyone could do by circumventing the validators, it's still good practice not to trust anything that has been on the client side. Here's the issue. No matter what I do, I can *never* get my server side validator to fire. It simply does not work.
Now, I have run the examples that are on MSDN, and they obviously work. There is something else going on here, some reason why it won't work in my page. I guess I could trawl through the script, and see what that reason is ( actually, this gives me a good excuse to install firebug ), but I don't have time, I will just add an OnClientClick to my submit button and do my validation there. If anyone knows either what I did wrong ( possible, obviously ), or what circumstances cause a custom validator to do nothing, even when it fires, please let me know.
Update - Wow. My OnClientClick does not fire, either. Only the server side click event will fire, looks like that is my only option for validation.
Further Update - Although I did initially try to make the validator part of my validation group, I suspect my use of validation groups is part of the problem. However, that does not explain why my OnClientClick event would not fire.