Solved

How to use validator for drop-down list

Posted on 2010-11-11
12
738 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 47
Error building VS2105 solution from repository 1 38
c#, case, if 4 21
aspx ascx, c# 7 19
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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