Well, the guy who has taught me everything I know about .NET, Bigyan, has taken off for a month to go back to Nepal to get married, leaving me with some very big shoes to fill while he is gone. While he is gone I have adopted a new philosophy that I am trying to follow, WWBD, What Would Bigyan Do?
This first thing that is necessary is to add a CustomValidator control to your page and to associate it to a control on the page by setting the ControlToValidate property.
Next, I would typically set the text of the CustomValidator control to an *. This is the text that shows up on the page where the custom validator exists. I normally position the CustomValidator either next to the control I am validating or next to the label for the control I am validating. The * then indicates to the user that there is an error with that field.
Following setting the text I set the ErrorMessage property of the CustomValidator to the error message I want to display. This message will then appear in the validation summary control if you have placed one on the page.
In my particular case I was validating a rich textbox control that was required. In my particular case the control would be blank either if it was actually blank or if it had "<p> </p>", hence I could not just use the RequiredFieldValidator. To code the server side check you use the ServerValidate event of the CustomValidator control and check the value property of the args objects that is passed in by the system. Perform your logic and if the value passes set args.IsValid = True, otherwise set it to False.
Protected Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If String.IsNullOrEmpty(args.Value) Or args.Value = "<p> </p>" Then
args.IsValid = False
args.IsValid = True
This function will then execute when a button that has its CausesValidation property set equal to true is clicked. Then, at any point in time if you want to check if all the validators on a page are valid you check if Page.IsValid is equal to true.
That’s it for handling the server side validation, but the next step is to handle the client side validation so that the client is not forced to look at a post back before knowing that they have an invalid input field.
function TxtQuestionValidate(sender, args)
if (args.Value == '' || args.Value == '<p> </p>')
args.IsValid = false;
args.IsValid = true;
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.