17

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;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
DJPB
  • 5,429
  • 8
  • 30
  • 44

4 Answers4

30

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.

Liam
  • 27,717
  • 28
  • 128
  • 190
Musa Hafalir
  • 1,752
  • 1
  • 15
  • 23
  • 2
    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 Hafalir Sep 03 '10 at 14:58
  • 1
    Yep, tried it just like you but still no success. What properties did you set when declaring the custom validator tag? – DJPB Sep 03 '10 at 15:30
  • solved it! that was it, probably was somethis with the previous code but any way, that's the solution, txs – DJPB Sep 03 '10 at 15:52
1

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
  • 8,541
  • 5
  • 29
  • 47
0

Here's my take on a server-side solution in C# to mimic the above answer, for anyone interested:

<asp:TextBox ID="txtStudentComments" runat="server" 
  Rows="8" Width="100%" 
  ToolbarCanCollapse="False" ValidationGroup="vg1" />
<asp:CustomValidator ID="cv1" runat="server" ControlToValidate="txtStudentComments" 
ErrorMessage="THESE COMMENTS DO NOT SEEM RIGHT. PLEASE REVIEW THEM AGAIN!" SetFocusOnError="true" 
Font-Bold="True" Font-Size="Medium" ValidationGroup="vg1" OnServerValidate="cv1_ServerValidate"></asp:CustomValidator>

And on the server:

//validate of the comment contains some specific words which imply the TET has not reviewed the comments!
    protected void cv1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CustomValidator cv = (CustomValidator)source;
        GridViewRow gvRow = (GridViewRow)cv.NamingContainer;
        TextBox editor = (TextBox)gvRow.FindControl("txtStudentComments");

        if (editor.Text.ToUpper().Contains("FACILITATOR TO INSERT COMMENTS HERE PLEASE"))
            args.IsValid = false;
        else
            args.IsValid = true;
    }

These two lines are the crux of it.

    CustomValidator cv = (CustomValidator)source;
    GridViewRow gvRow = (GridViewRow)cv.NamingContainer;

The NamingContainer will be a GridViewRow in my case, but it could be your entire page depending on your program. Either way it allows me to find the control I want, relative to the ControlToValidate object, which as mentioned will return the ClientID.

Fandango68
  • 4,461
  • 4
  • 39
  • 74
0

Here's my easy solution to be able to access the control to validate on client side. Add the regular Custom Validator control with the options you might need.

<asp:CustomValidator ID="cvalShippingRegionCountries" ErrorMessage="Choose a country" ClientValidationFunction="ClientValMultiSelectCountries" runat="server" Display="Dynamic" SetFocusOnError="true" /> 

Then, in code behind, just add a custom attribute to store the clientID of the control to validate.

cvalShippingRegionCountries.Attributes.Add("ControlToValidateClientID", multiselectShippingRegionCountries.ClientID);

Now, in the function that deals with the validation you can access the value like this:

function ClientValMultiSelectCountries(sender, args) {

        var multiselect = $find(sender.attributes.controltovalidateclientid.nodeValue);

        if ( #VALIDATION_CHECK_HERE# ) {
            args.IsValid = false;
        }
    }

You will get the clientID inside your function ;)

Hugo A.
  • 218
  • 2
  • 12