need help using customvalidator

2

2obvious

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.
 
T

Teemu Keiski

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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,166
Latest member
DollyBff32
Top