Simple problem with a Custom Validator

S

SimonH

Hi All,

I'm having problems getting a custom validator to work. I'm hoping someone
could perhaps notice what I've missed. I only want to provide a server side
validation method. The problem is basically that the method that I've designated
as the validation routine never gets called so I can't do anything about
potentially duff data.

I've declared the custom control as:

<asp:customvalidator id=valDoB runat="server" controltovalidate="txtDoB"
errormessage="Please ensure a valid date has been provided in the DoB field"
onservervalidate="valDoB_ServerValidate" EnableClientScript="False">*</asp:customvalidator>

Then I have a method called valDob_ServerValidate

public void valDoB_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs
args) {
if(txtDoB.Text == String.Empty){
valDoB.ErrorMessage = "Please ensure that the DoB field has been filled
in";
}
}


If anyone can spot what I've missed I would be very greatful

Thanks all

tce
 
W

William F. Robertson, Jr.

Couple things.

If you use an inner text and and error message, it won't work quite the
same. Stick with one or the other, unless you are using a summary
validator.

The CustomValidator will NOT call your validation method if the textbox is
empty. You will need to use a RequiredFieldValidator for this.

You validate method should look something like this. The args.IsValid
property actually determines whether validation fails for the page. It is
true by default, so you must make it false.

validate( .. )
{
if ( args.Value != "hello" )
args.IsValid = false;
}

HTH,

bill
 
S

SimonH

Hi Bill

Thanks for your reply. I'm not sure why but I went home last night with it
broken and then it started working on the first new build this morning.

Havent a clue why.

The info you've provided is helpful though so thank you

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top