Solved

How to disable requiredfieldvalidator from javascript

Posted on 2006-11-22
8
3,694 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:idealsw
8 Comments
 
LVL 1

Author Comment

by:idealsw
ID: 17996679
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17998356
could you please the post the client gets
0
 
LVL 1

Author Comment

by:idealsw
ID: 17998439
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 51

Expert Comment

by:ahoffmann
ID: 17999968
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
 
LVL 1

Author Comment

by:idealsw
ID: 18019501
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
 

Expert Comment

by:itakamo
ID: 18166647
.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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18328681
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now