Posts
16
Comments
106
Trackbacks
18
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. :)

posted on Friday, August 11, 2006 8:57 AM Print
Comments
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Paulo
8/14/2006 6:21 PM
Thanks!! :D
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
leandro koiti
8/18/2006 7:57 AM
you are the one! thanks a lot!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Zoria
9/1/2006 1:43 AM
it exists in 1.x
GREAT!!!!!!!THANKS!!!!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
None
10/10/2006 7:05 AM
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?
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Qazi
11/2/2006 10:37 PM
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.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Anand
1/31/2007 9:49 PM
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!!!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
oola
5/4/2007 11:49 AM
Great... thaks
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Naveed
5/7/2007 7:57 AM
wow wow wow
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Frank Frenzel
5/18/2007 8:28 PM
Thanks, you just saved me so much time!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
igal kakoon
6/3/2007 7:09 AM
as my mom always told me: "KISS"
keep it simple stupid
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Sanjib Saha
8/22/2007 10:20 PM
Is it work on asp.net 1.x
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
frances
9/26/2007 8:57 AM
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.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Asim
9/27/2007 9:29 PM
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
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Deepak Aggarwal
9/29/2007 11:11 PM
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.
Gravatar
# Re: Disabling an ASP.Net Validator through Javascript
Asim Sajjad
10/1/2007 10:18 PM
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
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Tony
10/31/2007 5:23 PM
Thanks
worked for me in 1.1
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
bali
1/10/2008 9:27 AM
Thanx. Works great!!!!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
bali
1/10/2008 9:27 AM
Thanx. Works great!!!!!!!!!!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Anish
1/16/2008 5:33 AM
Thanks yaar . ur really gr8
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Liron Cohen
1/20/2008 9:19 AM
And it's the first one in Google now :)
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Javier Callico
1/28/2008 8:23 AM
It worked for me too. Thanks.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
George
3/13/2008 7:25 AM
Thanks! saved me lots of time
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Rorigo
4/1/2008 6:06 PM
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);
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Anz
4/19/2008 6:54 AM
In the server the Page.IsValid will still be false.

http://www.FlickrMailer.com
Gravatar
# web project in asp.net 2.0
Ashwani Dwivedi
5/5/2008 2:25 AM
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
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Sonia
5/26/2008 10:07 PM
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.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
kpg
6/11/2008 1:03 PM
you are a gawd
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Rajasekhar
7/24/2008 5:25 AM
that's gr8
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Vipul
8/1/2008 11:31 PM
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
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
vishal parekh
8/6/2008 4:23 AM
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.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
phm
9/12/2008 11:07 AM
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?
Gravatar
# I have problem with the validation control inside update panel.
Hari
9/18/2008 5:34 AM
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..
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Hafiza
9/22/2008 12:17 AM
Its really gr8 .. but will this work if I call Page_ClientValidate();
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Ivan
9/24/2008 3:39 AM
Thanks - just what I was looking for
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Arsi
10/7/2008 5:52 AM
Thanks, first hit in google and just what i was looking for! :)
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
kk
10/18/2008 9:45 PM
How to disable entire validations on the page in one g0 through
javascript
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Kago
10/19/2008 2:43 AM
THANKS MAN!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Karthikeyan
12/5/2008 3:19 PM
Does someone know how to fire required field validators in the client side on events other than the associated ones.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Jonas
12/5/2008 3:36 PM
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. :)
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
saikrishna
1/26/2009 7:52 AM
it didn't work for me
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
beck
2/26/2009 2:41 AM
u are absolutely right...
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Cee Bee
3/26/2009 11:11 AM
This doesn't work ... :(
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Tore Aurstad
3/31/2009 10:24 AM
omfg U are a LIFE SAVER!!!!!!!!!!


:-)
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
DesperateProgrammer
4/6/2009 10:12 AM
You made my day!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Umersh
4/13/2009 10:19 AM
You are too good
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
chetas
4/22/2009 12:48 AM
thanks. Really it's nice.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Rasha
4/26/2009 5:20 AM
veryyyyyyyyyyyyyy amazing
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Muhammad Talal Shoaib
4/26/2009 7:18 AM
Thanks ...you are the best !!!!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Rizwan
5/5/2009 8:00 PM
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.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Mohamed Adel Elkhodary
6/8/2009 7:35 AM
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
Gravatar
# thanks a lot
ram
7/6/2009 12:32 AM
saved me time

Gravatar
# re: Disabling an ASP.Net Validator through Javascript
gBurgur
9/12/2009 8:38 AM
Strange but when I disable the validator by ValidatorEnable(myVal, false);

it makes the text box it is connected to disappear.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
gBurgur
9/12/2009 8:44 AM
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.
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
bunty
9/17/2009 4:00 AM
definitely appreciable.
Thanks buddy! saved lot of time
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
Fred
9/21/2009 5:48 PM
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!
Gravatar
# re: Disabling an ASP.Net Validator through Javascript
ali
12/23/2009 1:07 AM
thanx man ,,,saved my day,
keep up the good work

Post Comment

Title *
Name *
Email
Url
Comment *  
 
News