how can add validation 2 drowdownlist

Harun Ergün 260 Reputation points
2023-01-30T08:28:51.2766667+00:00

User's image

The user comes and selects the city from the first dropdownlist. If the first value is selected from the second dropdownlist, I want it to give an error. I want to add validation but failed.

Help me please ? Where is my mistake ? my code ;

var ikametSehir = $('#ikametSehir :selected').val();
var ikametIlce = $('#ikametIlce :selected').val();


if ('#ikametSehir :selected') {
                if ('#ikametIlce :selected', "İlçe Seçiniz") { 
                    $('#ikametIlce').focus()
                    alert('Lütfen İlçe Seçiniz')
                    return false;
            }
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,284 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
301 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,871 Reputation points Microsoft Vendor
    2023-01-30T09:56:58.1866667+00:00

    Hi @Harun Ergün,

    You can refer to the code below:

     <script src="Scripts/jquery-3.4.1.min.js"></script>
       <script type="text/javascript">       
           $(document).ready(function () {
               $("#ikametSehir").change(function () {
                   var val = $("#ikametSehir option:selected").text();
                  
                   alert(val);
                  
                   $("#ikametIlce").prop("disabled", false);
                   var val = $('#ikametIlce').val();
    
                   if (val == "0") {
                       alert("please select town");
                   }
               });
               $("#ikametIlce").change(function () {
                   var val = $('#ikametIlce').val();
                  
                   if (val == "0") {
                       alert("error");
                   }
               });
           });
       </script>
       
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
            <asp:DropDownList ID="ikametSehir" runat="server" Width="145px">
            
                <asp:ListItem Value="0">Afghanistan</asp:ListItem>
                <asp:ListItem Value="1">Albania</asp:ListItem>
                <asp:ListItem Value="2">Algeria</asp:ListItem>
               
            </asp:DropDownList>
           <asp:DropDownList ID="ikametIlce" runat="server" Width="145px" Enabled="false" >              
                <asp:ListItem Value="0">İlçe Seçiniz</asp:ListItem>
                <asp:ListItem Value="1">Albania</asp:ListItem>
                <asp:ListItem Value="2">Algeria</asp:ListItem>
                <asp:ListItem Value="3">American Samoa</asp:ListItem>
                
            </asp:DropDownList>
            
        </div>
        </form>
    </body>
    

    op

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful