Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc jquery dropdown validation

how i can validate dropdownlist with unobtrusive javascript ? As its validating textbox for required validator , but its not working for dropdownlist ?

Need to change unobtrusive js file for it ? or is there any other option to validate dropdownlist ?

i want to show errors when i checked form.validate() in my javascript .

like image 981
jon Avatar asked Feb 15 '26 13:02

jon


1 Answers

Markup

 <p>
        <label for="ProductCategoryList">Product Categories</label>
        <%= Html.DropDownList("ProductCategoryList", ViewData["ProductCategories"] as SelectList)%>
    </p>

And the validation script

        $.validator.addMethod("selectNone",
            function (value, element) {
                return this.optional(element) || element.selectedIndex != 0;
            },
           "Please select an option."
        );


        $(function () {
            $("#form1").validate({
                rules: {
                    ProductCategoryList: {
                        selectNone: true
                    }

                },
                messages: {
                    ProductCategoryList: {
                        selectNone: "This field is required"
                    }
                }
            });
        });
like image 77
naveen Avatar answered Feb 18 '26 07:02

naveen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!