Question : Repeater Enable/Disable RequiredFieldValidator depending a textbox


I have a repeater and I want to enable/disable a requiredFieldValidator depending on the value of a textbox.

<script type="text/javascript">
    function enableValidators (dateID, textID, valDate, valText) {
        if (document.getElementById(dateID).value != '' || document.getElementById(textID) != '') {
              ValidatorEnable(document.getElementById(valDate), true);
              ValidatorEnable(document.getElementById(valVisa), true);
         else {
            ValidatorEnable(document.getElementById(valDate), false);
            ValidatorEnable(document.getElementById(valVisa), false);

<asp:Repeater ID="repeaterID" runat="server"
    OnItemDataBound="repeaterID_ItemDataBound"  EnableViewState="True">
<table class="RubTabCoul" width="580px">
        <td >
            <asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("COL_DATE", "{0:dd.MM.yyyy}") %>' Width="63px"
           <asp:RequiredFieldValidator ID="rfvDate" runat="server" Display="Static"  
                                                            ControlToValidate="txtDate" Text="<img src='../images/warning.gif' />" ValidationGroup="valSave"></asp:RequiredFieldValidator>
        <td >
            <asp:TextBox ID="txtText" runat="server" Text='<%# Bind("COL_TEXT") %>' Width="79px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="rfvVisa" runat="server" Display="Static"  
                                                            ControlToValidate="txtText" Text="<img src='../images/warning.gif' />" ValidationGroup="valSave"></asp:RequiredFieldValidator>


protected void repeaterID_ItemDataBound(object sender, RepeaterItemEventArgs e)
        if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            TextBox text = (TextBox)e.Item.FindControl("txtText");
            TextBox date = (TextBox)e.Item.FindControl("txtDate");
            RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("rfvDate");
            RequiredFieldValidator val2 = (RequiredFieldValidator)e.Item.FindControl("rfvText");
            text.Attributes.Add("onchange", "enableValidators(" + text.ClientID + "," + date.ClientID + "," + val.ClientID + "," + val2.ClientID + ");");
            date.Attributes.Add("onchange", "enableValidators(" + text.ClientID + ", " + date.ClientID + ", " + val.ClientID + ", " + val2.ClientID + ");");

I become always a javascript error when I enter something in the a textbox :   'ctl00_plhContent_ctl00_...' is undefined

Thanks for your help

Answer : Repeater Enable/Disable RequiredFieldValidator depending a textbox

You can get an idea on how to enable disable RVF based on a textbox value.

But i think you are having issues on identifying the control in java script. This can happen if your text box control is within some other controls. Then you have to send the full control hierarchy to javascript code. Enable 'trace' option in ASP to identify the full path to your textbox and try to send that path to your javascript and see.
Random Solutions  
programming4us programming4us