How to get the ‘controlToValidate’ property on ClientValidationFunction?

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;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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.

Method 4

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 😉


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x