ASP.NET - dropdown validation in requirfield or any validation control

Asked By msakt on 19-Apr-12 03:15 AM
dropdown validation in requirfield or any validation control
Suchit shah replied to msakt on 19-Apr-12 03:29 AM

say you have dropdown like below

<asp:DropDownList runat="server" id="ddl"> <asp:ListItem Value="0" text="Select a Value"> .... </asp:DropDownList>

Then validation should like this

<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />

or
<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0".... >
 
the 2nd way is to use a compare validator:

<asp:CompareValidator ID="re1" runat="Server" ValueToComare="0" ConroltoCompare="ddl" Operator="Equel"....

Somesh Yadav replied to msakt on 19-Apr-12 04:37 AM
Hi
Try This code for validation


<asp:DropDownList runat="server" id="txtName" />
<asp:RequiredFieldValidator runat="server" id="Name" controltovalidate="txtName" errormessage="Please enter your name!" />
</asp:DropDownList>
Chintan Vaghela replied to msakt on 19-Apr-12 05:23 AM

Hello,

 

You achieve your task using Custom validation using following way

Made ur logic as follows

 

<asp:DropDownList ID="drpInner" runat="server">

      <asp:ListItem Text="Select" Value="0"></asp:ListItem>

      <asp:ListItem Text="A" Value="a"></asp:ListItem>

      <asp:ListItem Text="B" Value="b"></asp:ListItem>

      <asp:ListItem Text="C" Value="c"></asp:ListItem>

      <asp:ListItem Text="D" Value="d"></asp:ListItem>

    </asp:DropDownList>

 

 

    <asp:CustomValidator ID="CustomValidator1" ControlToValidate="drpInner" ClientValidationFunction="ClientValidate"

Display="Static" ErrorMessage="Select at least one!"

      ForeColor="green" Font-Name="verdana" Font-Size="10pt" runat="server" />

 

 

script type="text/javascript">

    function ClientValidate(source, arguments) {

 

      if (arguments.Value == '0')

        arguments.IsValid = false;

      else

        arguments.IsValid = true;

    }

</script>

 

 

 

Hope this helpful!

Thanks

 

dipa ahuja replied to msakt on 19-Apr-12 07:12 AM
 <asp:DropDownList ID="ddlaccesstype" runat="server" ValidationGroup="Chk" Width="171px">
    <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
    <asp:ListItem Text="Admin" Value="1"></asp:ListItem>
    <asp:ListItem Text="Supervisor" Value="2"></asp:ListItem>      
  </asp:DropDownList>
<asp:RequiredFieldValidator ID="reqFavoriteColor" Text="(Required)" InitialValue="0"
    ControlToValidate="ddlaccesstype" runat="server" />
 
Using javascript
 
<script type="text/javascript">
  function Validate() {
    var ddl = document.getElementById('<%=DropDownList1.ClientID%>');
    var option = ddl.options[ddl.selectedIndex];
    alert(option.value);
    if (option.value == 0) {
      alert('select value from dropdown');
      return false;
    }
    else {
      return true;
    }
  }
</script>
  <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate();" />