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

Discussion in 'ASP .Net' started by Alan Silver, Nov 2, 2005.

  1. Alan Silver

    Alan Silver Guest

    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
    ClientValidationFunction 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:CustomValidator ControlToValidate="txtDeliveryCountries"
    OnServerValidate="ValidateTxtDeliveryCountries"
    ClientValidationFunction="ValidateTxtDeliveryCountriesJS"
    Display="Dynamic" 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)
    Alan Silver, Nov 2, 2005
    #1
    1. Advertising

  2. Alan Silver

    Peter Blum Guest

    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 ValidateTxtDeliveryCountriesJS(source, args)
    {
    if (valid)
    args.IsValid = true;
    else
    args.IsValid = false;
    }

    Note that names like args and IsValid are case sensitive.

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "Alan Silver" <> wrote in message
    news:...
    > 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
    > ClientValidationFunction 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:CustomValidator ControlToValidate="txtDeliveryCountries"
    > OnServerValidate="ValidateTxtDeliveryCountries"
    > ClientValidationFunction="ValidateTxtDeliveryCountriesJS"
    > Display="Dynamic" 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, Nov 2, 2005
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    >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:


    Yup, that was my sloppy explanation. Sorry about that.

    >function ValidateTxtDeliveryCountriesJS(source, args)
    >{
    > if (valid)
    > args.IsValid = true;
    > else
    > args.IsValid = false;
    >}
    >
    >Note that names like args and IsValid are case sensitive.


    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

    >--- Peter Blum
    >www.PeterBlum.com
    >Email:
    >Creator of "Professional Validation And More" at
    >http://www.peterblum.com/vam/home.aspx
    >
    >"Alan Silver" <> wrote in message
    >news:...
    >> 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
    >> ClientValidationFunction 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:CustomValidator ControlToValidate="txtDeliveryCountries"
    >> OnServerValidate="ValidateTxtDeliveryCountries"
    >> ClientValidationFunction="ValidateTxtDeliveryCountriesJS"
    >> Display="Dynamic" 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)

    >
    >


    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Nov 2, 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. Matt
    Replies:
    14
    Views:
    4,096
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  2. =?Utf-8?B?dmlkeWE=?=
    Replies:
    1
    Views:
    750
    Kevin Spencer
    Jun 2, 2005
  3. Boss302
    Replies:
    0
    Views:
    1,043
    Boss302
    Nov 21, 2006
  4. Bogdan
    Replies:
    2
    Views:
    651
    Bogdan
    Jun 9, 2008
  5. Zoe Hart
    Replies:
    1
    Views:
    368
    Scott Wisniewski
    Jan 8, 2004
Loading...

Share This Page