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:

  1. Hello
    such a gud post..
    Thanks a lot.

    ReplyDelete
  2. Great little piece of code. Thank you!

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for your post..

    Thanks once again. :) :)

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

    ReplyDelete
  6. 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

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

    ReplyDelete
  8. 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

    ReplyDelete
  9. 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

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

    Also visit my blog ... life insurance worth it

    ReplyDelete
  11. 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

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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

    ReplyDelete
  14. 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 - -

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

    ReplyDelete
  16. 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

    ReplyDelete
  17. 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

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. 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


    ReplyDelete
  20. 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

    ReplyDelete
  21. For Python training in Bangalore, Visit:- Python training in Bangalore

    ReplyDelete
  22. 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


    ReplyDelete
  23. 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

    ReplyDelete
  24. Great example! This is a clear and practical guide for dynamically enabling/disabling validation in ASP.NET with JavaScript. It really simplifies handling client-side validation based on user input. Thanks for sharing!
    online internship | internship in chennai | online internship for students with certificate | bca internship | internship for bca students | sql internship | online internship for btech students | internship for 1st year engineering students

    ReplyDelete