Newbie question: how to programmatically enable and disable client side validation

D

Dave Caton

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
 
G

Guest

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.
 
D

Dave Caton

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
 
P

Peter Blum

Hi Dave,

I rewrote ASP.NET validation to overcome its many limitations. For a list of
those limitations, see http://www.peterblum.com/vam/valmain.aspx.
My "Professional Validation And More" provides a new property on each of its
22 validators where you establish a rule that enables or disables it
intelligently. You can set the rule to look at a checkbox if you like. It
works fully on the client-side, with support for many more browsers than
Microsoft's validators. You can set it up in Visual Studio.net design mode.

Details are at: http://www.peterblum.com/vam/home.aspx

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

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
 
G

Guest

Dave
Yes you can.

For example if you create a html checkbox, an asp textbox, an asp button and a requiredfiled validator as below

<INPUT onclick="test();" id="chk" style="Z-INDEX: 101; LEFT: 360px; POSITION: absolute; TOP: 264px"
type="checkbox">
<asp:RequiredFieldValidator id="rf" style="Z-INDEX: 102; LEFT: 448px; POSITION: absolute; TOP: 264px" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 304px"
runat="server"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 664px; POSITION: absolute; TOP: 296px" runat="server"
Text="Button"></asp:Button>

And then create a client side function called test() as below


function test(){
var val = document.Form1.chk.checked;
ValidatorEnable(rf,val);
}

the required field validator will be enabled or disabled on the client depending on the checkbox value and the page will not be posted if disabled. Make sure to call function test() also on the 'onload' event of the 'body' tag.

The validatorEnable function is a built in client side script which enables or disables the validator control.

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
 
G

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top