views:

132

answers:

2

Hi everybody

Lets say I have this code.

<asp:TextBox ID="TextBox1" runat="server" />

<asp:CustomValidator ID="CustomValidator1" runat="server"
    ClientValidationFunction="ValidationFunction1"
    ControlToValidate="TextBox1"
    Display="Dynamic" />

And a validationFunction:

function ValidationFunction1(sender, args)
{
}

And i would like to know if, inside the function I could get the Control to validate something like:

var v = sender.ControlToValidate;
+1  A: 

Not verified, just a hint:

var v = document.getElementById('<%=CustomValidator1.FindControl(CustomValidator1.ControlToValidate).ClientID>%');

of course you could simply do it like:

var v = document.getElementById('<%=TextBox1.ClientID%>');

if you know exactly what you're validating. The first method is good when the control to be validated is set dynamically and you don't know beforehand which one it will be.

Also FindControl() might return null so you'd need to test for that too in order to avoid an exception.

Hope this helps.

CyberDude
+1. You were reading my mind.
Steven
+2  A: 

Actually sender.controltovalidate gives the ClientID of the control. So this seems like a solution.

function ValidationFunction1(sender, args){
    var v = document.getElementById(sender.controltovalidate);
}

I tried and it worked for me. Please notify if it works.

Musa Hafalır
Hi Musa. I tried to do that but it's returning 'undefined'
DJPB
It is really interesting because I have tried it in IE and Chrome, both worked fine in my .NET 3.5 web site. Please be sure that you have the sender.controltovalidate lowercase because I also got undefined with pascalcased ControlToValidate.
Musa Hafalır
Yep, tried it just like you but still no success. What properties did you set when declaring the custom validator tag?
DJPB
solved it! that was it, probably was somethis with the previous code but any way, that's the solution, txs
DJPB