How do I use client-side validation with a custom validator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Silver

    How do I use client-side validation with a custom validator?

    Hello,

    I have a custom validator on my page, and have the server-side code
    working fine. I want to add a client-side funtion as well, but am not
    sure how to wire it in so that it works with the other validators on the
    page.

    I specified the name of the Javascript function with the
    ClientValidatio nFunction attribute of the custom validator, and it is
    being called fine. However, if the validator returns false (ie bad
    data), nothing happens on the client. With the other validators, I get a
    message box popping up with the warnings, and I get the red text by the
    control so the user can see which control fired the error.

    How do I get the custom validator to do this? Here is the server tag...

    <asp:CustomVali dator ControlToValida te="txtDelivery Countries"
    OnServerValidat e="ValidateTxtD eliveryCountrie s"
    ClientValidatio nFunction="Vali dateTxtDelivery CountriesJS"
    Display="Dynami c" Text="At least one country name does not start with
    U_, E_ or W_" Runat="Server" />

    (the point of the validator is that it checks a multiline textbox, that
    is supposed to contain a list of countries, each of which has one of U_,
    E_ or W_ as a prefix. The validator checks to make sure they didn't
    enter a country without a prefix, or one with an invalid prefix).

    TIA

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
  • Peter Blum

    #2
    Re: How do I use client-side validation with a custom validator?

    Hi Alan,

    You wrote: "if the validator returns false". The definition of the
    client-side validation function requires that you set the args parameter's
    IsValid property to true or false:

    function ValidateTxtDeli veryCountriesJS (source, args)
    {
    if (valid)
    args.IsValid = true;
    else
    args.IsValid = false;
    }

    Note that names like args and IsValid are case sensitive.

    --- Peter Blum

    Email: PLBlum@PeterBlu m.com
    Creator of "Profession al Validation And More" at


    "Alan Silver" <alan-silver@nospam.t hanx> wrote in message
    news:bOd8YWCmDK aDFwla@nospamth ankyou.spam...[color=blue]
    > Hello,
    >
    > I have a custom validator on my page, and have the server-side code
    > working fine. I want to add a client-side funtion as well, but am not sure
    > how to wire it in so that it works with the other validators on the page.
    >
    > I specified the name of the Javascript function with the
    > ClientValidatio nFunction attribute of the custom validator, and it is
    > being called fine. However, if the validator returns false (ie bad data),
    > nothing happens on the client. With the other validators, I get a message
    > box popping up with the warnings, and I get the red text by the control so
    > the user can see which control fired the error.
    >
    > How do I get the custom validator to do this? Here is the server tag...
    >
    > <asp:CustomVali dator ControlToValida te="txtDelivery Countries"
    > OnServerValidat e="ValidateTxtD eliveryCountrie s"
    > ClientValidatio nFunction="Vali dateTxtDelivery CountriesJS"
    > Display="Dynami c" Text="At least one country name does not start with U_,
    > E_ or W_" Runat="Server" />
    >
    > (the point of the validator is that it checks a multiline textbox, that is
    > supposed to contain a list of countries, each of which has one of U_, E_
    > or W_ as a prefix. The validator checks to make sure they didn't enter a
    > country without a prefix, or one with an invalid prefix).
    >
    > TIA
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)[/color]


    Comment

    • Alan Silver

      #3
      Re: How do I use client-side validation with a custom validator?

      >Hi Alan,[color=blue]
      >
      >You wrote: "if the validator returns false". The definition of the
      >client-side validation function requires that you set the args parameter's
      >IsValid property to true or false:[/color]

      Yup, that was my sloppy explanation. Sorry about that.
      [color=blue]
      >function ValidateTxtDeli veryCountriesJS (source, args)
      >{
      > if (valid)
      > args.IsValid = true;
      > else
      > args.IsValid = false;
      >}
      >
      >Note that names like args and IsValid are case sensitive.[/color]

      I was setting args.Valid, not args.IsValid, which is why it didn't work.
      I changed it and it worked fine.

      Thanks very much
      [color=blue]
      >--- Peter Blum
      >www.PeterBlum.com
      >Email: PLBlum@PeterBlu m.com
      >Creator of "Profession al Validation And More" at
      >http://www.peterblum.com/vam/home.aspx
      >
      >"Alan Silver" <alan-silver@nospam.t hanx> wrote in message
      >news:bOd8YWCmD KaDFwla@nospamt hankyou.spam...[color=green]
      >> Hello,
      >>
      >> I have a custom validator on my page, and have the server-side code
      >> working fine. I want to add a client-side funtion as well, but am not sure
      >> how to wire it in so that it works with the other validators on the page.
      >>
      >> I specified the name of the Javascript function with the
      >> ClientValidatio nFunction attribute of the custom validator, and it is
      >> being called fine. However, if the validator returns false (ie bad data),
      >> nothing happens on the client. With the other validators, I get a message
      >> box popping up with the warnings, and I get the red text by the control so
      >> the user can see which control fired the error.
      >>
      >> How do I get the custom validator to do this? Here is the server tag...
      >>
      >> <asp:CustomVali dator ControlToValida te="txtDelivery Countries"
      >> OnServerValidat e="ValidateTxtD eliveryCountrie s"
      >> ClientValidatio nFunction="Vali dateTxtDelivery CountriesJS"
      >> Display="Dynami c" Text="At least one country name does not start with U_,
      >> E_ or W_" Runat="Server" />
      >>
      >> (the point of the validator is that it checks a multiline textbox, that is
      >> supposed to contain a list of countries, each of which has one of U_, E_
      >> or W_ as a prefix. The validator checks to make sure they didn't enter a
      >> country without a prefix, or one with an invalid prefix).
      >>
      >> TIA
      >>
      >> --
      >> Alan Silver
      >> (anything added below this line is nothing to do with me)[/color]
      >
      >[/color]

      --
      Alan Silver
      (anything added below this line is nothing to do with me)

      Comment

      Working...