Solved

How to use validator for drop-down list

Posted on 2010-11-11
12
722 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

18 Experts available now in Live!

Get 1:1 Help Now