Solved

How to use validator for drop-down list

Posted on 2010-11-11
12
727 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

8 Experts available now in Live!

Get 1:1 Help Now