Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > need help using customvalidator

Reply
Thread Tools

need help using customvalidator

 
 
2obvious
Guest
Posts: n/a
 
      08-12-2004
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.
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      08-12-2004
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/de...pplusvalid.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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a customvalidator in a Gridview tokuzumi@gmail.com ASP .Net 0 04-04-2007 06:34 PM
Using customvalidator control in asp.net 1.1 settyv@gmail.com ASP .Net 1 10-26-2006 04:41 PM
Using CustomValidator anony ASP .Net 2 09-24-2004 02:19 PM
Using CustomValidator to valid url QueryString Martin ASP .Net 1 06-16-2004 02:47 PM
Using CustomValidator to report unexpected errors Stephen Miller ASP .Net 2 02-16-2004 08:55 PM



Advertisments