Yes you can.
If you have a html checkbox, an asp textbox, an asp button and a requiredfield validator as below:
<INPUT onclick="test();" id="chk" style="Z-INDEX: 101; LEFT: 360px; POSITION: absolute; TOP: 264px" type="checkbox">
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 304px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rf" style="Z-INDEX: 102; LEFT: 448px; POSITION: absolute; TOP: 264px" runat="server" ErrorMessage="Required" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 664px; POSITION: absolute; TOP: 296px" runat="server" Text="Button"></asp:Button>
And a client side function as below:
<script language="javascript">
function test()
{
var val = document.Form1.chk.checked;
ValidatorEnable(rf,val);
}
</script>
The RequiredFieldValidator will be enabled or disabled depending on the checkbox value on the client side rather than posting the page.
Please make sure to call the function also on the onload event of the body tag as below.
<body onload="test();" MS_POSITIONING="GridLayout">
The ValidatorEnable function is a built in function which takes a client-validator and a Boolean value.
Enables or disables a client validator.
Being disabled will stop it from evaluating and it will always appear valid.
Hope this helps
--
Ibrahim
Dave Caton said:
Hi Ibrahim,
is it possible to disable the validator without a postback to the server?
Dave
Ibrahim Shameeque said:
Dave
You can make the checkbox's autopostback property to 'true' and on the
checkchanged event of checkbox, you can enable or disable to
requiredfieldvalidator control (like a toggle switch). This will allow you
to post the page when the checkbox is not checked.
--
Ibrahim
Dave Caton said:
Hi all,
I'm developing a web form that a requirement to validate that the contents
of a textbox is not empty but only if a checkbox is checked. The situation
is a little like the following:
[x] Email me when the results are ready
Email Address: [_________________________]
In the above example, I only want to test that the "email address" field is
not blank if the "Email me when the results are ready" checkbox is checked.
Any advice would be most appreciated.
Cheers,
Dave