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

A

Alan Silver

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
 
P

Peter Blum

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: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
A

Alan Silver

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: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top