dropdownlist with customvaliation

G

gane kol

Hi

I have a custom validator that validates a dropdownlist with a listitem
value as "". But the clientvalidation function is never called.
but if i use value = "0" and check it in javascript, it works fine. It might
be silly, but it is eating my time. Any Ideas?

for eg:
<asp:DropDownList id=DropDownList1 runat="server">
<asp:ListItem Value="">select</asp:ListItem>
<asp:ListItem Value="1">test1</asp:ListItem>
<asp:ListItem Value="2">test2</asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator id=CustomValidator1 runat="server"
ControlToValidate="DropDownList1" ErrorMessage="errrrrrrr"
ClientValidationFunction="testddl"></asp:CustomValidator>

function testddl(source, arguments) { var ddlDropDownList1 =
document.Form1.DropDownList1.options[document.Form1.DropDownList1.selectedIn
dex].value; if (ddlDropDownList1 == "") { arguments.IsValid = false; } }

Thanks
gane
 
S

samuelrobertson

Hey,

This is by Microsoft's design. The only way to get around it is to
write your own custom validator control.

If you look at WebUIValidation.js (in C:\Inetpub) you'll find this
function is being called:

function CustomValidatorEvaluateIsValid(val) {
var value = "";
if (typeof(val.controltovalidate) == "string") {
value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
}
var args = { Value:value, IsValid:true };
if (typeof(val.clientvalidationfunction) == "string") {
eval(val.clientvalidationfunction + "(val, args) ;");
}
return args.IsValid;
}

Notice it returns true (valid) if the value is an empty string.

Oh, btw, welcome to Microsoft hell :).
 
P

Peter Blum

I agree that using the RequiredFieldValidator with its InitialValue property
is the way to go. I just wanted to correct the statement made below.

By design, the CustomValidator will not get called if the control it is
evaluating has a blank value. So you simply do not assign the
ControlToValidate property. It will now call your evaluation method at all
times and you are responsible for getting the value yourself.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
S

sam

This is a good point. In fact, if you look at the JS code above, you
can see this for yourself.

I knew this at one point, but who can keep all the catch-alls,
exceptions, and special cases straight? ;).

-Sam
 

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

Top