Jonas Bush

Blog.blog.blog.blog
posts - 17 , comments - 190 , trackbacks - 17

Disabling an ASP.Net Validator through Javascript

If you ever find yourself needing to selectively disable an asp.net validator through javascript, you can do the following (in 2.0, not sure if this exists in 1.x):

function doSomething()
{
  var myVal = document.getElementById('myValidatorClientID');
  ValidatorEnable(myVal, false); 

}

Quick and easy! Sadly, not as easy to find through Google, so hopefully this post will help that. :)

Print | posted on Friday, August 11, 2006 8:57 AM | Filed Under [ ASP.Net ]

Feedback

Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks!! :D
8/14/2006 6:21 PM | Paulo
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

you are the one! thanks a lot!
8/18/2006 7:57 AM | leandro koiti
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

it exists in 1.x
GREAT!!!!!!!THANKS!!!!
9/1/2006 1:43 AM | Zoria
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

This is great. However, I would like to change the status of a control from invalid to valid rather than disable the validator. Any idea how I can do that?
10/10/2006 7:05 AM | None
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Great it works fine and best example so far, good work but be sure while getting the id of any control with this new master/content page setup.
11/2/2006 10:37 PM | Qazi
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

It did not work for me. I had another solution of setting these three properties.

function disableValidator()
{
var myval = document.getElememtById('validator.ClientId');
myval.style.cssText="";
myval.style.display='none';
myval.style.accelerator=true;
}

It worked perfectly!!!
1/31/2007 9:49 PM | Anand
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Great... thaks
5/4/2007 11:49 AM | oola
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

wow wow wow
5/7/2007 7:57 AM | Naveed
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks, you just saved me so much time!
5/18/2007 8:28 PM | Frank Frenzel
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

as my mom always told me: "KISS"
keep it simple stupid
6/3/2007 7:09 AM | igal kakoon
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Is it work on asp.net 1.x
8/22/2007 10:20 PM | Sanjib Saha
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

function doSomething()
{
var myVal = document.getElementById('fvr');
ValidatorEnable(myVal, false);
}

I do not know whether I understand the myValidatorClientID well. I put my validation control name there. since after the end user click the check box, the text box show up. I have the validation control for the text box enable.
9/26/2007 8:57 AM | frances
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

The "ValidatorEnable" function i have used but it did't work, my problem is that i am using the dropdownlist and in the onchange event of the dropdown i have added client side script which check the specific value and on the bases of that value the regularexpressionvalidator enable/disable but my problme did't solved, i have tried many hours but :( no luchk any one can help me
thanks in advance
9/27/2007 9:29 PM | Asim
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Asim, if you are using a drop down and validation is not working set the CauseValidation property of the dropdown to true and it will start working.

The Problem with the posters solution. Even If the validation is disabled using the code, but the server throws back error for requirefieldvaldiator control. So the validator has to be handled on the server side as well.
9/29/2007 11:11 PM | Deepak Aggarwal
Gravatar

# Re: Disabling an ASP.Net Validator through Javascript

Look my real scenario is that , i have dropdownlist which contain the value for the credit card type, mean visa, Master Card, discover and etc , and i have a text box in which user can enter the Credit Card number, when the "Visa" is selected from the dropdownlist then the regularexpressionValidator validate the text box value other wise not
thanks in advance
10/1/2007 10:18 PM | Asim Sajjad
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks
worked for me in 1.1
10/31/2007 5:23 PM | Tony
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanx. Works great!!!!
1/10/2008 9:27 AM | bali
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanx. Works great!!!!!!!!!!
1/10/2008 9:27 AM | bali
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks yaar . ur really gr8
1/16/2008 5:33 AM | Anish
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

And it's the first one in Google now :)
1/20/2008 9:19 AM | Liron Cohen
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

It worked for me too. Thanks.
1/28/2008 8:23 AM | Javier Callico
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks! saved me lots of time
3/13/2008 7:25 AM | George
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

This is great, thanks, helped me a lot.

I hope this helps some of you who have their validators nested within other controls.

var myVal = document.getElementById('<%=Page.FindControl("myValidatorID").ClientID%>');

ValidatorEnable(myVal, true);
4/1/2008 6:06 PM | Rorigo
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

In the server the Page.IsValid will still be false.

http://www.FlickrMailer.com
4/19/2008 6:54 AM | Anz
Gravatar

# web project in asp.net 2.0

I have created web project in visual studio 2.0 on my pc i work properly but when i open that project from other pc it gives error public token key etc how do i create project in share level sothat more than one user can work at a time on that project
5/5/2008 2:25 AM | Ashwani Dwivedi
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

I have problem with ValidatorEnable(myval,true).
Problem is that when i enable the validator on some client event which even doesn't cause postback, validation error message is displayed to me.
5/26/2008 10:07 PM | Sonia
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

you are a gawd
6/11/2008 1:03 PM | kpg
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

that's gr8
7/24/2008 5:25 AM | Rajasekhar
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

The "ValidatorEnable" function i have used but it did't work, my problem is that i am using the dropdownlist and in the onchange event of the dropdown i have added client side script which check the specific value and on the bases of that value the regularexpressionvalidator enable/disable but my problme did't solved

i change CauseValidation property of dropdown then in server side pagr.isvalid give false then waht to do?
thanks in advance
8/1/2008 11:31 PM | Vipul
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

thanks,

thanks a lot

excellent work done,not just to findout it,but solution put like this straightforward ,

great, i searched google, and this is the firstlink i found.

again thank you very much.
8/6/2008 4:23 AM | vishal parekh
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

What i want to do is just enabling or disabling my validators on client side. But I want to keep the validation itself on server side.

I tried to use
- ValidatorEnable(validator, bEnabled);
in order to enabled / disabled on client script my validators.

It works for System.Web.UI.WeControls.RequiredFieldValidator validators, but it does not for my own validators, which derive from BaseValidator ...

Any idea?
9/12/2008 11:07 AM | phm
Gravatar

# I have problem with the validation control inside update panel.

I have 4 reqiuired field validation control inside a gridview which is inside an update panel. When i try to save the grid without enter any data it show the validation summary says that you have to enter values for these fields. But my problem is the error message show 4 times.. Anyone help me to figure my issue.. I am running out of time..
9/18/2008 5:34 AM | Hari
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Its really gr8 .. but will this work if I call Page_ClientValidate();
9/22/2008 12:17 AM | Hafiza
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks - just what I was looking for
9/24/2008 3:39 AM | Ivan
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks, first hit in google and just what i was looking for! :)
10/7/2008 5:52 AM | Arsi
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

How to disable entire validations on the page in one g0 through
javascript
10/18/2008 9:45 PM | kk
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

THANKS MAN!
10/19/2008 2:43 AM | Kago
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Does someone know how to fire required field validators in the client side on events other than the associated ones.
12/5/2008 3:19 PM | Karthikeyan
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Karthikeyan,

I addressed that in this blog post:

http://geekswithblogs.net/jonasb/archive/2007/01/29/104893.aspx

Thanks :)

BTW, I'm amazed to have this many comments so long after writing the post. :)
12/5/2008 3:36 PM | Jonas
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

it didn't work for me
1/26/2009 7:52 AM | saikrishna
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

u are absolutely right...
2/26/2009 2:41 AM | beck
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

This doesn't work ... :(
3/26/2009 11:11 AM | Cee Bee
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

omfg U are a LIFE SAVER!!!!!!!!!!


:-)
3/31/2009 10:24 AM | Tore Aurstad
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

You made my day!
4/6/2009 10:12 AM | DesperateProgrammer
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

You are too good
4/13/2009 10:19 AM | Umersh
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

thanks. Really it's nice.
4/22/2009 12:48 AM | chetas
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

veryyyyyyyyyyyyyy amazing
4/26/2009 5:20 AM | Rasha
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks ...you are the best !!!!
4/26/2009 7:18 AM | Muhammad Talal Shoaib
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

It didn't work for me. I made sure I had the function name (ValidatorEnable) in correct case (if it was case-sensitive). I even put the alert() statements to make sure the code was being executed. I see that the code is being executed, but the validator is not disabled.

I finally resorted to use Custom Validator and took care of conditional validation in ServerValidate event.
5/5/2009 8:00 PM | Rizwan
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

You can use the enabled property of the validators. It works for me in dot net framework 2.0

var requiredFieldValidatorPrice = document.getElementById("<%= requiredFieldValidatorPrice.ClientID %>");
requiredFieldValidatorPrice.enabled = true;

I hope It helps
6/8/2009 7:35 AM | Mohamed Adel Elkhodary
Gravatar

# thanks a lot

saved me time

7/6/2009 12:32 AM | ram
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Strange but when I disable the validator by ValidatorEnable(myVal, false);

it makes the text box it is connected to disappear.
9/12/2009 8:38 AM | gBurgur
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Someone suggested in a comment above just disabling the validator and that worked for me.

document.getElementById("rev_txtCmp_Item_Name").disabled = true;

Thanks for this discussion.
9/12/2009 8:44 AM | gBurgur
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

definitely appreciable.
Thanks buddy! saved lot of time
9/17/2009 4:00 AM | bunty
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks. Its pretty cool when you search for something you think is going to be hard to find, yet the first google result is a 2 second solution to it. thanks again!
9/21/2009 5:48 PM | Fred
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

thanx man ,,,saved my day,
keep up the good work
12/23/2009 1:07 AM | ali
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks its work gr8 but on Button_click Page.IsValid should be false
2/11/2010 10:39 PM | Alisha
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Works great! Took your idea to a function...
<pre>
function toggleValidator(myRVF, state) {
var myVal = document.getElementById(myRVF);
ValidatorEnable(myVal, state);
}
</pre>
3/9/2010 11:14 AM | Drew
Gravatar

# W3C Validtator

Nice information, I really appreciate the way you presented.Thanks for sharing..

5/12/2010 4:45 AM | w3cvalidation
Gravatar

# W3C Validator

Nice information, I really appreciate the way you presented.Thanks for sharing..
5/14/2010 2:24 AM | w3cvalidation
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Hi , really this is very useful information. But can anyone let me know that where I need to call this function doSomething()?
I would really appreciate any once response.
Thanks :)
5/26/2010 10:17 PM | Ankit
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

That doSomething function would be called wherever you need to disable the validators. So if you have some logic/circumstances where the validators aren't valid, that's where you'd call it.
5/26/2010 10:19 PM | Jonas
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks Jonas for your response. My Validator control is having property Display="Dynamic" and it is related to a textbox, so the error message start displaying at the sametime when I start entering anything wrong (special character) in that text box. Shall I call this function at the textbox event OnTextChanged?
5/26/2010 10:24 PM | Ankit
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

If you're wanting to disable the validator selectively when the user starts typing, then yes. The display=dynamic attribute means that the validator's error message will only show when it is necessary.
5/26/2010 10:26 PM | Jonas
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks Jonas, It's working fine now.. :) :) :)

u made my day.. :)
5/26/2010 11:02 PM | Ankit
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

<pre>
function toggleValidator(myRVF, state) {
var myVal = if(document.getElementById(<%=myRVF.ClientID%>").value=="")
{
document.getElementById(<%=myRVF.ClientID%>").focus();
return false;
}
ValidatorEnable(myVal, state);
}
</pre>
6/5/2010 2:30 AM | Viju
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Apparently, if you run the built-in web server from Visual Studio (aka Cassini) then you may need the following solution:

"Enabling Cassini to use Client-Side Validation in ASP.NET"
By Zarko Gajic
http://delphi.about.com/od/adptips2005/qt/cassinivalidate.htm

Visual Studio 2008 SP 1 had this issue.
6/18/2010 7:49 AM | T. Shane Whittet
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

ValidatorEnable...works perfectly..wow :)))
6/22/2010 12:41 AM | Saurabh
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Instead of the solution I mentioned above, I have installed IIS and run the following to get Cassini to work:
@C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -i
@C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis -c
6/24/2010 11:21 AM | T. Shane Whittet
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thank you
7/1/2010 11:49 PM | Prince Mathew
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Really quick and easy.
Thanks
8/2/2010 5:46 AM | Nazmi Altun
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Works great for setting the value to false, does not seem to work setting it to true
9/30/2010 8:24 AM | drytin
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Great, saved my time
11/14/2010 6:07 AM | Kumar
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

fantastic D:
11/23/2010 3:56 PM | anon
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks for posting this is very helpful and quickly
12/15/2010 7:55 PM | Ivan
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

I tried Disabling an ASP.Net Validator through Javascript using my own function because I couldn't find any online. But I didn't get the code right, and the only forum that discussed this said I had to download some exe files and install a script to get it to work. I'm very happy I eventually found your blog, it worked flawlessly from the first try.
4/28/2011 4:48 AM | Donald Huisinga
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Nice, Just what the doctor ordered.

Thanks for posting/sharing...
5/3/2011 10:15 AM | Moe
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Concise, and to the point. This will save some time!
6/6/2011 10:54 AM | Ian
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thanks, so great very easy
8/23/2011 5:03 PM | omar
Gravatar

# With JQUERY doesn't work,why?

var validator = document.getElementById('<%=RequiredFieldValidator6.ClientID %>'); // works
var validator = $('#<%=RequiredFieldValidator6.ClientID %>'); // not
ValidatorEnable(validator, false);
9/1/2011 4:18 AM | Mindaugas
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

It works and disable the validator.
However it ignore the validate but it is visible in form!!!
12/7/2011 11:41 AM | Maryam
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

I used "ValidatorUpdateDisplay" function to change the visibility.

function disValidators() {
var val = document.getElementById("reqApply");
ValidatorEnable(val, false);
ValidatorUpdateDisplay(val);
}
function enaValidators() {
ValidatorEnable(document.getElementById("reqApply"), true);
}
function ValidatorUpdateDisplay(val) {
if (typeof(val.display) == "string") {
if (val.display == "None") {
return;
}
if (val.display == "Dynamic") {
val.style.display = val.isvalid ? "none" : "inline";
return;
}
}
val.style.visibility = val.isvalid ? "hidden" : "visible";
12/7/2011 12:24 PM | Maryam
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

asp.net is not showing validatorEnable , what should i do to make it work?
im using this code in <head> like this but it aint working.

<script type="text/javascript" language="javascript" >
function EnableDisableValidation()
{
var myVal = document.getElementById('UserIDRequiredFieldValidator');
ValidatorEnable(myVal, true);
}
1/12/2012 10:00 PM | Janzi
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

It is working in JavaScript that good what is not working in asp.net code for page.Isvalid please check.
1/26/2012 9:44 PM | ranjit
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

not goog
4/10/2012 3:58 AM | werwer
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thank you Very Much for such a wonderful Post..
This Helped me a lot..
5/6/2012 2:30 AM | KiranKumar Roy
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Thank you so much :)
9/6/2012 1:57 AM | Meghana
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Nice article. Thanks for posting. i have found one more please check

http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=60&title=%20%20Dynamically%20Enable%20or%20Disable%20Validator%20and%20Page.IsValid%20Using%20C#%20In%20ASP.NET
11/1/2012 8:50 PM | vinay
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Dude .... u simply saved me! THANKS A LOT.
11/9/2012 4:56 PM | Rakesh
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

but when postback occurs, it enable again & prevent postback.
Plz help.
12/10/2012 11:33 PM | cseppd
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

requiredfield validator is inside repeater control and i'm calling it on onclick event.. it doesn't work..
1/7/2013 6:58 AM | pg
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Great it works for me. Thanks a lot
2/8/2013 4:52 AM | aalok kumar dahayat
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

var emailEmpty = $("#registerContent").find('[id$=email_blank]');
ValidatorEnable(emailEmpty, false);


It isn't working for me... Does someone know why???
7/26/2013 3:40 AM | Norman
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

var emailEmpty = $("#registerContent").find('[id$=email_blank]');
ValidatorEnable(emailEmpty, false);


It isn't working for me... Does someone know why???
7/26/2013 3:40 AM | Norman
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Its Work Perfectly...Nice Solution..
(1)Which One fsater Javascript or Asp.net Validation?
(2)Which one faster if javascript disable then Asp.net Validation or we do using code for validation.
3/5/2014 7:09 AM | Samir Khimani
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Nice Solution..
(1)Which One fsater Javascript or Asp.net Validation?
(2)Which one faster if javascript disable then Asp.net Validation or we do using code for validation.
3/5/2014 7:10 AM | Samir Khimani
Gravatar

# re: Disabling an ASP.Net Validator through Javascript

Norman, I guess you have to get the DOM-element not the jQuery-element. Like so:

var emailEmpty = $("#registerContent").find('[id$=email_blank]')[0];
ValidatorEnable(emailEmpty, false);
9/11/2014 1:29 AM | Jan
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: