Google Search

Google
 

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 Asp.net 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)
{
if(type=="Email")
{
document.getElementById('Email').style.display="block";
document.getElementById('PhoneNo').style.display="none";
ValidatorEnable(document.getElementById("RequiredFieldValidator1"), true);
ValidatorEnable(document.getElementById("RequiredFieldValidator2"), false);

}
else
{
document.getElementById('Email').style.display="none";
document.getElementById('PhoneNo').style.display="block";
ValidatorEnable(document.getElementById("RequiredFieldValidator1"), false);
ValidatorEnable(document.getElementById("RequiredFieldValidator2"), true);
}
}
</script>

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>
<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"/>
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ValidationGroup="vgSubmit"/>

35 comments:

Rohit Puranik said...

Hello
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

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 ... http://www.faadhsdgfuyewertwe2.com/gjhsdfg.html

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: www.thebookshelfoperation.com

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

Unknown 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

Yogayogi said...

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
python Training institute in Chennai
python Training institute in Bangalore
python Training in Pune

Suresh said...
This comment has been removed by the author.
Kavi said...

Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
Devops Training in Chennai | Devops Training Institute in Chennai


service care said...

Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
motorola service center near me
motorola mobile service centre in chennai
moto g service center in chennai

Benish said...

Nice post...Thanks for sharing.
Python training in Chennai
Python training in OMR
Python training in Velachery
Python certification training in Chennai
Python training fees in Chennai
Python training with placement in Chennai
Python training in Chennai with Placement
Python course in Chennai
Python Certification course in Chennai
Python online training in Chennai
Python training in Chennai Quora
Best Python Training in Chennai
Best Python training in OMR
Best Python training in Velachery
Best Python course in Chennai

Anonymous said...

For Python training in Bangalore, Visit:- Python training in Bangalore

jasonbob said...

curry 6
yeezy shoes
coach outlet store online
goyard wallet
curry shoes
nike lebron shoes
air max 90
yeezy 500
cheap jordans
yeezy boost
coach outlet store
xiaofang20191205

Realtime Experts said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.Informatica Training in Bangalore


yanmaneee said...

yeezy
moncler jackets
golden goose
goyard
adidas yeezy
goyard handbags
supreme hoodie
golden goose outlet
yeezy supply
supreme t shirt

nisharoshan said...

thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!

Web Designing Training Course in Chennai | Certification | Online Training Course | Web Designing Training Course in Bangalore | Certification | Online Training Course | Web Designing Training Course in Hyderabad | Certification | Online Training Course | Web Designing Training Course in Coimbatore | Certification | Online Training Course | Web Designing Training Course in Online | Certification | Online Training Course



radhika said...

hanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!

AWS training in Chennai

AWS Online Training in Chennai

AWS training in Bangalore

AWS training in Hyderabad

AWS training in Coimbatore

AWS training

lavanya said...

Great postsalesforce training in chennai

software testing training in chennai

robotic process automation rpa training in chennai

blockchain training in chennai

devops training in chennai

salome said...

its really useful
devops Training in chennai | devops Course in Chennai

IamLinkfeeder said...

David Forbes is president of Alliance Marketing Associates IncIamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder

Pavithra Devi said...

This post is so interactive and informative.keep update more information...
PHP Training in Tambaram
PHP Training in Tambaram

sheatheaud said...

these details Visit Your URL browse around this website important link the original source check this link right here now

Unknown said...

good quality replica bags h22 s2z68y8a35 high quality replica bags q04 b9z26f8l90 replica ysl bags o95 q6p64i3e64

Mishra A said...

Your writing is incredibly captivating; I found it hard to put down! The eloquence with which you convey ideas is truly engaging and enthralling.
Best Software Training Institute In Electronic City Bangalore