-1

I created dropdown and i want to validate this.. I am struggling with doing drop down validation.

<div class="editor-field">
            <% if (ViewData["Country"] != null) { %>
                <%: Html.DropDownList("Country", "Any")%>
            <%} else { %>
                <select id="Country" name="Country"></select>
            <%} %>

            <% if (ViewData["State"] != null) { %>
                <%: Html.DropDownList("State", "Any")%>
            <%} else { %>
                <select id="State" name="State"></select>
            <%} %>

            <% if (ViewData["City"] != null) { %>
                <%: Html.DropDownList("City", "Any")%>
            <%} else { %>
                <select id="City" name="City"></select>
            <% } %>

            <% if (ViewData["Region"] != null) { %>
                <%: Html.DropDownList("Region", "Any")%>
            <% } else { %>
                <select id="Region" name="Region"></select>
            <% } %>
       </div>

This is my drop down box code.. How to validate in jquery?

5
  • What type of validation(s) are you gonna apply? Commented Jul 20, 2013 at 4:50
  • @Duk, hey, you can refer this stackoverflow.com/questions/12467682/… Commented Jul 20, 2013 at 4:50
  • They all look like they can be null, what are you going to check. If you use strongly typed models you can enable client side validation and use data annotations to describe validation checkout this tutorial asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-6 Commented Jul 20, 2013 at 4:54
  • @n-p-subedi required field validation.. Commented Jul 20, 2013 at 5:04
  • Please do not use the jquery-validate tag unless your question is about this particular plugin. It's consider "tag-spam". Edited. Thanks. Also, please at least show an attempt at solving this, rather than simply asking for code. Commented Jul 20, 2013 at 16:47

2 Answers 2

0

Assign common class to all select boxes, and then through that common class get the active ids and validate them. Hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

0

Using Jquery is probably the best way to validate HTML Controls.

JS:

$("#btnSubmit").click(function () {

            if ($("#ddlView :selected").val() == 0) {
                alert("Please select a Course");
            }
            else {
                alert("Success !! You have selected Course : " + $("#ddlView :selected").text()); ;
            }
        });

HTML:

<select id="ddlView">
        <option value="0">Select</option>
        <option value="1">Java Script</option>
        <option value="2">CSS</option>
        <option value="3">JQuery</option>
        <option value="3">HTML</option>
        <option value="3">C#</option>
    </select>

<input type="button" class="btn" value="Submit" id="btnSubmit" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.