need help using customvalidator

Discussion in 'ASP .Net Web Controls' started by 2obvious, Aug 12, 2004.

  1. 2obvious

    2obvious Guest

    I picked up a really nifty function that allows me to count "words" in
    a string of text.

    CountWords()

    I'd like to use this function to validate some form fields. One field
    should have no more than 100 words, the other should have no more than
    20.

    I can't think of a way to do this in ASP.NET

    <asp:customvalidator id="OurCustomValidator"
    ControlToValidate="OurTextBox"
    OnServerValidate="CountWords(20)"
    ErrorMessage="Error"
    runat="server" />

    won't work, because I can't pass arguments to CountWords() in ASP.NET
    using this approach.

    If I wrote two functions, one that checked for 100 words, the other
    that checked for 20 (I know: totally bad practice)

    <asp:customvalidator id="OurCustomValidator"
    ControlToValidate="OurTextBox"
    OnServerValidate="CountWords1()"
    ErrorMessage="Error"
    runat="server" />

    I couldn't do anything with the results. I must return Page.IsValid,
    not some random true/false value.

    <asp:comparevalidator id="validator2"
    ControlToValidate="CountWords(OurTextBox)"
    Operator="LessThanEqual"
    ControlToCompare="100"
    ErrorMessage="Error"
    runat="server" />

    Can I even /do/ this (use my control as a function parameter)?

    If anyone knows of a way to do this (I could keep guessing up theories
    forever, please someone who /knows/) it would be much appreciated.
     
    2obvious, Aug 12, 2004
    #1
    1. Advertising

  2. 2obvious

    Teemu Keiski Guest

    Hi,

    Yes, you cannot give parameter that way because signatures of event handlers
    (which OnServerValidate adds to ServerValidate event) are predefined.

    But why don't you use CustomValidator behaviour which lets you validate
    multiple controls at once. Just leave ControlToValidate blank and
    CustomValidator fires the server-side validation as usually (also at client
    if you have client-side function specified, it would fire for each submit
    try), you just need to locate the controls you want to validate, and you
    could have the validation logic in the validation method. If you absolutely
    want to parameterize the allowed length, you could write this quite easily
    also as completely standalone validator control (inherit from BaseValidator)

    You can check this article for more information

    ASP.NET Validation In Depth
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspplusvalid.asp

    Let me know if I can be of more assistance,

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke



    "2obvious" <> wrote in message
    news:...
    > I picked up a really nifty function that allows me to count "words" in
    > a string of text.
    >
    > CountWords()
    >
    > I'd like to use this function to validate some form fields. One field
    > should have no more than 100 words, the other should have no more than
    > 20.
    >
    > I can't think of a way to do this in ASP.NET
    >
    > <asp:customvalidator id="OurCustomValidator"
    > ControlToValidate="OurTextBox"
    > OnServerValidate="CountWords(20)"
    > ErrorMessage="Error"
    > runat="server" />
    >
    > won't work, because I can't pass arguments to CountWords() in ASP.NET
    > using this approach.
    >
    > If I wrote two functions, one that checked for 100 words, the other
    > that checked for 20 (I know: totally bad practice)
    >
    > <asp:customvalidator id="OurCustomValidator"
    > ControlToValidate="OurTextBox"
    > OnServerValidate="CountWords1()"
    > ErrorMessage="Error"
    > runat="server" />
    >
    > I couldn't do anything with the results. I must return Page.IsValid,
    > not some random true/false value.
    >
    > <asp:comparevalidator id="validator2"
    > ControlToValidate="CountWords(OurTextBox)"
    > Operator="LessThanEqual"
    > ControlToCompare="100"
    > ErrorMessage="Error"
    > runat="server" />
    >
    > Can I even /do/ this (use my control as a function parameter)?
    >
    > If anyone knows of a way to do this (I could keep guessing up theories
    > forever, please someone who /knows/) it would be much appreciated.
     
    Teemu Keiski, Aug 12, 2004
    #2
    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. Stephen Miller
    Replies:
    2
    Views:
    699
    Peter Blum
    Feb 16, 2004
  2. Martin
    Replies:
    1
    Views:
    421
    Martin
    Jun 16, 2004
  3. anony

    Using CustomValidator

    anony, Sep 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    635
    Ken Dopierala Jr.
    Sep 24, 2004
  4. Replies:
    1
    Views:
    393
    Brennan Stehling
    Oct 26, 2006
  5. Replies:
    0
    Views:
    443
Loading...

Share This Page