Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

field validation on two controls-how? 1

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I have two dropdowns(lets say dd1, dd2)...

i want to put required field validators on them...but here is the condition...

one of the dropdowns should be selected as empty...

i mean...

dd1:
item 1: ""
item 2: "blah"
item 3: "foo"

dd2:
item 1: ""
item 2: "vsd"
item 3: "bar"

now what i want is...one of the selected items should be ""...if they selected dd1 as "" and dd2 as "vsd"...thats fine...but if they select dd1 as "blah" and dd2 as "bar"...thats not fine...and error message should show up saying that only one item from either dropdowns should be selected...

any suggestions...

-DNG
 
I think you would have to use 2 customvalidator controls (1 on each control) and write your own code to check the value of each. Or have an custom validation function which does the same job when the form is posted.

Or you could have boths dropdowns performing a postback when the selection is changed and causing the other one to be reset to "" and disabled (though this is a different approach)

just some ideas...
 
front end...
<asp:dropdownlist id=dd1 runat=server onchange="setDDLVals(this,formName.dd2);" />

<asp:dropdownlist id=dd2 runat=server onchange="setDDLVals(this,formName.dd1);" />

javascript...
function setDDLVals(ddl,that)
{
if (ddl.selectedIndex > 0) {
that.selectedIndex = 0;
};
}

this way no serverside code or validation required! No postback needed, dont worry if onchange underlines as non existent member of dropdownlist in designer.
 
You should really check that the conditions are met server-side once the form is submitted. adamroof's suggestion is perfectly acceptable to go along side this server side validation (in fact it's recommended as if javascript is enabled it will save posting back to the server) however, if any of your users have javascript disabled then they will bypass the validation. e.g.
Code:
If DropDownList1.SelectedItem.Value = "" And DropDownList2.SelectedItem.Value = "" Then
            ' Not Allowed
        End If


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Who doesnt enable javascript? Especially in an ASP.Net Application? Major functionality would dissapear.

Write the app for the intended user base. Causing alerts and popups saying the "user is an idiot and should have not done that" hurts their feelings. Preventing the issue in the first place should be main priority.

Ive in my experience avoided RFV's and coded in controls to quide the user. Instead, i hide the submit button until all fields are populated to my requirements. Do the best you can to provide clear instructions on what to fill in.

Just my 2 pennies
 
thanks adamroof...looks promising to me...will try it...

thanks all...

-DNG
 
Who doesnt enable javascript?
According to web statistics around 10% of users either don't have javascript turned on or are using a browser that doesn't support javascript.

You should always validate user data server-side period. If you want to add client-side code to assist in this validation then that's fine and will be helpful but it should not be relied on to validate data as it will not work in 100% of cases (whereas serv-side validation will).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top