Solved

Javascript adn required field validation asp.net

Posted on 2010-11-09
3
433 Views
Last Modified: 2012-06-21
hi,

i am using a required field validation on my dropdownlist. i have two drop downs and would like to hide one of the validators if both items have not been filled out. There should be a way to do this with javascript or JQuery. not sure how to do the test.

Thanks
0
Comment
Question by:JMRFan4Life
3 Comments
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 200 total points
ID: 34094584
Can you give exact scenario when you want to disable.
The idea is you get the two dropdown list by IDs in javascript and then see if the value for them is nothing or some pre-assigned, if so call ValidatorEnable method and disable the validator you want to.

Something like here: http://weblogs.asp.net/gurusarkar/archive/2010/07/26/enable-disable-validator-controls-from-javascript.aspx

If you can provide some of your code, we can get better idea.
0
 
LVL 13

Accepted Solution

by:
devlab2012 earned 300 total points
ID: 34094843
I assume that you have two validators val1 and val2. And a button to submit the form. Try the following implementation:

1. Add onclientclick="custValidate()" for the button.

3. The code for custValidate() function will be:

function custValidate()
{
//enable both validators
ValidatorEnable(val1, true);
ValidatorEnable(val2, true);

//run the validation
ValidatorValidate(val1);
ValidatorValidate(val2);

//check if both the validations fail, disable any one of these
if (val1.isvalid == false && val2.isvalid == false)
{
ValidatorEnable(val2, false);
ValidatorValidate(val2);
}

}
0
 

Author Closing Comment

by:JMRFan4Life
ID: 34094966
I actually used some javascript to get this done. below is my code actually used, but the examples above work jsut as well.

            if (ddlFPGenderChild1.selectedIndex == 0 && ddlFPRelationChild1.selectedIndex == 0) {
              $(rfvrel1valid).css({ "visibility": "visible", "display": "none" });
            };

i use selectedIndex to determine if the dropdowns have been selected or not. This case if first position is shown for both drop downs then hide one.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now