How to disable requiredfieldvalidator from javascript

I have a problem completely disabling client side validation when using MulitViews and ValidationSummary.

In my application, I have a textbox which is disabled until the value of another control enables it.  When it is enabled, it requires a value.  I  wanted to use as much of asp.net as possible and therefore added a RequiredFieldValidator.

After loading the page, I run a javascript routine to disable any textboxes which shouldn't be enabled and also use ValidatorEnable to disable validation on that textbox.  However, when I switch views, the validator summary displays the errormessage.

I've put together an example to demonstrate this:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script>
    function chkvalidators()
    {
    if (document.getElementById('rfv1') != null)
        {alert('rfv1 found!');
        ValidatorEnable(rfv1,false);
        }
    if (document.getElementById('rfv2') != null)
        {alert('rfv2 found!');
        ValidatorEnable(rfv2,false);
        }
    }
    </script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:MultiView ID="MultiView1" runat="server">
            <asp:View ID="View1" runat="server">
                view 1<br />
                <br />
                <br />
                <asp:TextBox ID="TextBox1" runat="server" Enabled="False"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="TextBox1"
                    ErrorMessage="Type text in view1"></asp:RequiredFieldValidator>&nbsp;
                <asp:Button ID="Button1" runat="server" Text="Switch to 2" />
                <input id="Button4" type="button" value="chkvalidators" onclick="chkvalidators();"/><br />
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
            </asp:View>
            <asp:View ID="View2" runat="server">
                view 2<br />
                <br />
                <asp:TextBox ID="TextBox2" runat="server" Enabled="False"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfv2" runat="server" ControlToValidate="TextBox2"
                    Enabled="False" ErrorMessage="Type text in view2"></asp:RequiredFieldValidator>
                <asp:Button ID="Button2" runat="server" Text="Switch to 1" />
                <input id="Button3" type="button" value="chkvalidators" onclick="chkvalidators();"/><br />
                <asp:ValidationSummary ID="ValidationSummary2" runat="server" />
            </asp:View>

        </asp:MultiView></div>
    </form>
   
    <script>
    chkvalidators();
    </script>
</body>
</html>

In theory, switching between the two views should not validate anything, but the validationsummary displays the error message.

Any ideas how to fix this?

Many thanks

Ian
LVL 1
idealswAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Computer101Connect With a Mentor Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
idealswAuthor Commented:
oh forgot the code behind to switch views:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MultiView1.ActiveViewIndex = 0
    End Sub
0
 
ahoffmannCommented:
could you please the post the client gets
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
idealswAuthor Commented:
This is what is rendered to the client when view1 showing:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
      Untitled Page
</title>
    <script>
    function chkvalidators()
    {
    if (document.getElementById('rfv1') != null)
        {alert('rfv1 found!');
        ValidatorEnable(rfv1,false);
        }
    if (document.getElementById('rfv2') != null)
        {alert('rfv2 found!');
        ValidatorEnable(rfv2,false);
        }
    }
    </script>
   
</head>
<body>
    <form name="form1" method="post" action="Default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIwOTA3OTA5NzZkGAEFCk11bHRpVmlldzEPD2RmZNr7cXhApSS+DkbaHtRqX03WqnK5" />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>


<script src="/onlinesurvey/WebResource.axd?d=OZNLojH0oYIoSGaeEfcieg2&amp;t=632965890535486348" type="text/javascript"></script>


<script src="/onlinesurvey/WebResource.axd?d=0WN_QYZF7Zp2IqoNQaNezJ20Ko9lcsSMkJcNRwxXYAM1&amp;t=632965890535486348" type="text/javascript"></script>
<script type="text/javascript">
<!--
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
// -->
</script>

    <div>
        &nbsp;
                view 1<br />
                <br />
                <br />
                <input name="TextBox1" type="text" id="TextBox1" disabled="disabled" />
                <span id="rfv1" style="color:Red;visibility:hidden;">Type text in view1</span>&nbsp;
                <input type="submit" name="Button1" value="Switch to 2" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button1&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="Button1" />
                <input id="Button4" type="button" value="chkvalidators" onclick="chkvalidators();"/><br />
                <div id="ValidationSummary1" style="color:Red;display:none;">

</div>
            </div>
   
<script type="text/javascript">
<!--
var Page_ValidationSummaries =  new Array(document.getElementById("ValidationSummary1"));
var Page_Validators =  new Array(document.getElementById("rfv1"));
// -->
</script>

<script type="text/javascript">
<!--
var rfv1 = document.all ? document.all["rfv1"] : document.getElementById("rfv1");
rfv1.controltovalidate = "TextBox1";
rfv1.errormessage = "Type text in view1";
rfv1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
rfv1.initialvalue = "";
// -->
</script>

<div>

      <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKX+/HcBgLs0bLrBgKM54rGBvUpQdSf/5ynVRdvmuCmur7RtqEm" />
</div>

<script type="text/javascript">
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
    ValidatorOnLoad();
}

function ValidatorOnSubmit() {
    if (Page_ValidationActive) {
        return ValidatorCommonOnSubmit();
    }
    else {
        return true;
    }
}
// -->
</script>
        </form>
   
    <script>
    chkvalidators();
    </script>
</body>
</html>

and this is for view2




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
      Untitled Page
</title>
    <script>
    function chkvalidators()
    {
    if (document.getElementById('rfv1') != null)
        {alert('rfv1 found!');
        ValidatorEnable(rfv1,false);
        }
    if (document.getElementById('rfv2') != null)
        {alert('rfv2 found!');
        ValidatorEnable(rfv2,false);
        }
    }
    </script>
   
</head>
<body>
    <form name="form1" method="post" action="Default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIwOTA3OTA5NzZkGAEFCk11bHRpVmlldzEPD2QCAWThaoefe8I5gvHwuU4KE9UfLZum9Q==" />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>


<script src="/onlinesurvey/WebResource.axd?d=OZNLojH0oYIoSGaeEfcieg2&amp;t=632965890535486348" type="text/javascript"></script>


<script src="/onlinesurvey/WebResource.axd?d=0WN_QYZF7Zp2IqoNQaNezJ20Ko9lcsSMkJcNRwxXYAM1&amp;t=632965890535486348" type="text/javascript"></script>
<script type="text/javascript">
<!--
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
// -->
</script>

    <div>
        &nbsp;
                view 2<br />
                <br />
                <input name="TextBox2" type="text" id="TextBox2" disabled="disabled" />
                <span id="rfv2" style="color:Red;visibility:hidden;">Type text in view2</span>
                <input type="submit" name="Button2" value="Switch to 1" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button2&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="Button2" />
                <input id="Button3" type="button" value="chkvalidators" onclick="chkvalidators();"/><br />
                <div id="ValidationSummary2" style="color:Red;">
      <ul><li>Type text in view1</li></ul>
</div>
            </div>
   
<script type="text/javascript">
<!--
var Page_ValidationSummaries =  new Array(document.getElementById("ValidationSummary2"));
var Page_Validators =  new Array(document.getElementById("rfv2"));
// -->
</script>

<script type="text/javascript">
<!--
var rfv2 = document.all ? document.all["rfv2"] : document.getElementById("rfv2");
rfv2.controltovalidate = "TextBox2";
rfv2.errormessage = "Type text in view2";
rfv2.enabled = "False";
rfv2.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
rfv2.initialvalue = "";
// -->
</script>

<div>

      <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK2g5eiDwLs0fbZDAK7q7GGCFheUrVHbKmJtZKU09+lPYGFv8z+" />
</div>

<script type="text/javascript">
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
    ValidatorOnLoad();
}

function ValidatorOnSubmit() {
    if (Page_ValidationActive) {
        return ValidatorCommonOnSubmit();
    }
    else {
        return true;
    }
}
// -->
</script>
        </form>
   
    <script>
    chkvalidators();
    </script>
</body>
</html>

Cheers
Ian
0
 
ahoffmannCommented:
in your javascript you're referingtoelemnt id which do not exist, for example:
   document.getElementById('rfv1'

You need to use the ids of the tags, for example
    TextBox1
0
 
idealswAuthor Commented:
I found out what was going on.  Even though the clientside validation was disabled, the server side validation was still doing it's validation.  Disabling the rfv on the serverside and enabling it on the clientside does the trick nicely.

                        rfv2.EnableClientScript = True
                        rfv2.Enabled = False

Best wishes
Ian
0
 
itakamoCommented:
.NET makes an array on validators.  You can access this array and the validator with this code:
document.all["rfv"].enabled = true;

Best wishes
Paco
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.