Solved

How to use validator for drop-down list

Posted on 2010-11-11
12
746 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
[X]
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
  • 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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