Google Search


Tuesday, February 26, 2008

Enable/Disable RequiredFieldValidator with Javascript

Sometimes we need to Enable or Disable validation on client side.For that use ValidatorEnable function in the javacsript Script Library.

For that set EnableClientScript property of validator to True.

Here i give example for this:

I have a page with a couple of radio buttons.On radio button selection i want to enable/disable validation.

In example if i select Email radio button then Email div will display and only txtEmail textbox validator is enabled.

Java Script for this:

<script language="JavaScript" type="text/javascript">
function autoSelect(control,type)
ValidatorEnable(document.getElementById("RequiredFieldValidator1"), true);
ValidatorEnable(document.getElementById("RequiredFieldValidator2"), false);

ValidatorEnable(document.getElementById("RequiredFieldValidator1"), false);
ValidatorEnable(document.getElementById("RequiredFieldValidator2"), true);

Code for this:

Email :
<input type="radio" id="RadioButton1" runat="server" value="Plan1" name="Plan" onclick="autoSelect(this,'Email')" checked />
PhoneNo :
<input type="radio" id="RadioButton2" runat="server" value="Plan1" name="Plan" onclick="autoSelect(this,'PhoneNo')" />
<div id="Email">
Email <asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Email Required"
ControlToValidate="txtEmail" EnableClientScript="true" ValidationGroup="vgSubmit" />
<div id="PhoneNo" style="display:none">
PhoneNo <asp:TextBox ID="txtPhoneNo" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="PhoneNo Required"
ControlToValidate="txtPhoneNo" EnableClientScript="true" ValidationGroup="vgSubmit"/>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ValidationGroup="vgSubmit"/>


Rohit Puranik said...

such a gud post..
Thanks a lot.

Anonymous said...

Great little piece of code. Thank you!

Eran Smith said...
This comment has been removed by the author.
karthick said...

Thanks for your post..

Thanks once again. :) :)

Anonymous said...

ValidatorEnable(document.getElementById("RequiredFieldValidator2"), true);
Causes an error message imidiatly, when user do nothing yet

Tips Tricks Ideas said...

Android is a mobile operating system that has been highly successful because of its incredible features. Most of the smart phones in the market run on Android OS. Since the success of Android, lot of Android Development Companies has spurred up everywhere. We are also putting good logic to develop more in this field
tachnologys basket

Anonymous said...

I used to be suggested this blog by my cousin.

I'm not positive whether this publish is written by way of him as no one else know such precise approximately my problem. You are amazing! Thanks!

Feel free to surf to my blog post waist to height ratio calculator

Anonymous said...

Spot on with this write-up, I really feel this amazing site needs a lot more attention.
I'll probably be returning to read through more, thanks for the information!

Here is my web-site ...

Anonymous said...

I don't know whether it's just me or if perhaps everyone
else encountering issues with your blog. It appears as if some of the
text on your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them too?
This may be a issue with my browser because I've had this happen previously. Thanks

my web-site advance

Anonymous said...

You're so cool! I do not think I've read through a single thing
like that before. So great to find someone with genuine thoughts on
this issue. Really.. thanks for starting this up.
This website is something that is required on the web, someone
with a bit of originality!

Here is my web site:

Anonymous said...

Great post. I will be experiencing many of these issues as well.

Also visit my blog ... life insurance worth it

Anonymous said...

Hello mates, good article and good urging commented at this place,
I am really enjoying by these.

Here is my web site :: plumber training dublin

Anonymous said...

What's up, I would like to subscribe for this web site to take most recent updates, so where can i do it please help out.

my web site :: best online casino usa

Anonymous said...

Thank you for the good writeup. It in reality was a
amusement account it. Glance advanced to far added agreeable from you!
By the way, how can we keep in touch?

Feel free to visit my blog post - usa online casino

Anonymous said...

Thank you for some other wonderful article. Where else may anyone get that kind of information in such an ideal manner of writing?
I've a presentation next week, and I'm on the search for such information.

my homepage no deposit rtg casinos - -

Anonymous said...

electronic cigarettes reviews, smokeless cigarettes, e cigarette, e cigarette health, electronic cigarette reviews, e cigarette reviews

Pavithra M said...

Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai velachery | dot net training institute in velachery

Mithun Mithun said...

It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.. dot net training and placement in chennai | best dot net training in chennai

Pavel Co Ebele said...

Very much useful article. Kindly keep blogging

Java Training in Chennai

Java Online Training India

Kingsly David said...

Great post! This is very useful for me and gain more information, Thanks for sharing with us.
Web Designing Training in Chennai

pooja said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
Devops Training courses
python Training in chennai
Devops Training in Bangalore