Simple problem with a Custom Validator

Discussion in 'ASP .Net' started by SimonH, Jan 24, 2005.

  1. SimonH

    SimonH Guest

    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
     
    SimonH, Jan 24, 2005
    #1
    1. Advertising

  2. 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
    "SimonH" <> wrote in message
    news:...
    > 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
    >
     
    William F. Robertson, Jr., Jan 24, 2005
    #2
    1. Advertising

  3. SimonH

    SimonH Guest

    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

    > 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
    > "SimonH" <> wrote in message
    > news:...
    >> 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
    >>
     
    SimonH, Jan 25, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z. f.

    custom validator problem

    z. f., Aug 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,396
    DujHoD
    Aug 3, 2004
  2. bruce barker
    Replies:
    1
    Views:
    600
    z. f.
    Aug 3, 2004
  3. Replies:
    4
    Views:
    905
    Richter~9.6
    Feb 13, 2007
  4. Scott Yenor

    How to get custom control to work with custom validator

    Scott Yenor, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    213
    Scott Yenor
    Jan 27, 2004
  5. SimonH

    Simple problem with a Custom Validator

    SimonH, Jan 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    117
    SimonH
    Jan 25, 2005
Loading...

Share This Page