Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to use validator for drop-down list

Posted on 2010-11-11
12
735 Views
Last Modified: 2013-12-17

Hello group,

I'm using a drop-down list on a form (aspx page) where it has following items as value:

index 0 = "Select a value"
index 1 = "Value 1"
index 2 = "Value 2"
index 3 = "Value 3"

Now, my question is that can I use a validator in front-end (rather than writing a C# code) to make sure user is not saving the form with index 0 chosen?

Thanks!
0
Comment
Question by:akohan
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34112959
What type of validation do you want to perform?
0
 

Author Comment

by:akohan
ID: 34113049

that if index = 0 then show a message/warning to user to select any values from 1 to 3 but 0.

0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 63 total points
ID: 34113275
Use a RangeValidator to ensure the user selects an option between 1 and 3 (or an appropriate range based on the number of items in your list).
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:DropDownList ID="dropDown" runat="server">
			<asp:ListItem Text="Select an Option" Value="0" />
			<asp:ListItem Text="Value 1" Value="1" />
			<asp:ListItem Text="Value 2" Value="2" />
			<asp:ListItem Text="Value 3" Value="3" />
		</asp:DropDownList>
		<asp:RangeValidator ID="dropDownValidator" runat="server" ControlToValidate="dropDown"
			EnableClientScript="true" ErrorMessage="Select an Option" MaximumValue="3" MinimumValue="1" />
		<hr />
		<asp:Button ID="SaveButton" runat="server" Text="Save" />
    </div>
    </form>
</body>
</html>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:akohan
ID: 34113669


Thanks Tgerbert,

I used it but it is not working the way I want it. I think I'm missing something here pelase advise:

Basically my control in front-end is something like

            <asp:DropDownList ID="dropDown" runat="server">
            </asp:DropDownList>

and ListItem values are read from a table in runtime so as I was looking at Source Page I can see:


<select name="ctl00$ctl00$cph1$cph1$ctrlCustomerRegister" style="width:137px;">
                              <option value="A">Select One:</option>
                              <option value="E">Value 1</option>
                              <option value="R">Value 2</option>
                              <option value="S">Value 3</option>
 </select>

The problem is that when I choose a value from 1 to 3  I get a message as "You must select an option."

What am I missing here?

Thanks.


0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34113856
Well, as you can see your values are A, E, R and S, so yeah - if you setup the validator to require a value between 1 and 3 it's gonna complain. ;)
Use a CustomValidator to check that the selected option is not the first one in the list.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
	<script language="javascript" type="text/javascript">
		function ValidateDropDown(source, arguments) {
			var dropdown = document.getElementById('<%= dropDown.ClientID %>');
			if (dropdown.selectedIndex == 0)
				arguments.IsValid = false;
			else
				arguments.IsValid = true;
		}
	</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:DropDownList ID="dropDown" runat="server">
			<asp:ListItem Text="Select an Option" Value="0" />
			<asp:ListItem Text="Value 1" Value="1" />
			<asp:ListItem Text="Value 2" Value="2" />
			<asp:ListItem Text="Value 3" Value="3" />
		</asp:DropDownList>
		<asp:CustomValidator ID="dropDownValidator" runat="server" 
			ControlToValidate="dropDown" ErrorMessage="Select an Option" ClientValidationFunction="ValidateDropDown" />
		<hr />
		<asp:Button ID="SaveButton" runat="server" Text="Save" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34114927
You use RequireFieldValidator and set InitialValue property to the value you want to validate against:

<asp:RequiredFieldValidator  ID="RequiredFieldValidator  1" runat="server" ErrorMessage="*Select A Value"  ControlToValidate="YourDDLID"  InitialValue="A"/>

See the  InitialValue="A" ...
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34114964
0
 
LVL 8

Expert Comment

by:cubaman_24
ID: 34119670
Hello:
As esay as this.
best regards
<asp:RequiredFieldValidator ID="rfvMyDropDown" runat="server" ControlToValidate="myDropDown"
                            Text="(*)"></asp:RequiredFieldValidator>

Open in new window

0
 
LVL 1

Assisted Solution

by:NileshThakkar
NileshThakkar earned 62 total points
ID: 34122823
Hi

As you mentioned, below will be your dropdown list code:

 <asp:DropDownList ID="cboSignInType" runat="server">
                                            <asp:ListItem Text="--Select a value--" Value="0"></asp:ListItem>
                                            <asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
                                            <asp:ListItem Text="Value 3" Value="3"></asp:ListItem>


And here  is validator which will fulfill your requirement. NO EXTRA code is required.

      <asp:RequiredFieldValidator ID="rqdSignInUserType" runat="server" ControlToValidate="cboSignInType" ValidationGroup="second" Display="Dynamic" ErrorMessage="*" InitialValue="0" EnableClientScript="true"></asp:RequiredFieldValidator>


Vote this if this answers your question
0
 

Author Comment

by:akohan
ID: 34243161

I will get back to you soon.
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34429101
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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