This is a migrated thread and some comments may be shown as answers.

Dropdown validation problem

1 Answer 366 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Season
Top achievements
Rank 1
Season asked on 31 Mar 2014, 05:43 AM
hi,
i want validate my dropdown list, just i want to check the dropdown have any value..
please give a solution
my code is
<form method="post" action="/UploadAthletes/UploadReturn" name="second" id="second" enctype="multipart/form-data">
                                <div id="crt">
                                    <label for="Coach" class="col-lg-4 col-sm-5 col-xs-11">Select Coach</label>
                                    <span class="k-invalid-msg" data-for="name"></span>
                                    <ul>
                                        <li style="padding-left:10px;" class=" col-lg-4 col-sm-5 col-xs-11">
                                            @(Html.Kendo().DropDownList()
                                      .Name("sp")
                                      .HtmlAttributes(new { style = "width:100%" })
                                      .OptionLabel("Select Coach...")
                                                      .DataTextField("CoachName")
                                                      .DataValueField("CoachID")
                                      .DataSource(source =>
                                      {
                                          source.Read(read =>
                                          {
                                              read.Action("GetCoach", "UploadAthletes");
                                          });
                                      })
                                            )
                                        </li>
                                    </ul>
                                </div>


1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 03 Apr 2014, 07:50 AM
Hi Anand,

I would recommend using a Kendo UI Validator with a custom validation rule that checks the widget's value. For example:  
<div class="demo">
    @(Html.Kendo().DropDownList()
          .Name("products")
          .DataTextField("ProductName")
          .DataValueField("ProductID")
          .OptionLabel(" ")
          .DataSource(source => {
              source.Read(read =>
              {
                  read.Action("GetProducts", "Home");
              });
          })
    )
    <span data-for="products" class="k-invalid-msg"></span> //this span specifies the validation tooltip position
</div>
 
<script>
    $(".demo").kendoValidator({
        rules: {
            hasValue: function(input){
                if (input.is("[name='products']")) {
                    var dropdownlist = input.data("kendoDropDownList");
                    if (!dropdownlist.value()) {
                        return false;
                    }
                }
                return true;
            }
        },
        messages: {
            hasValue: "Please select"
        }
    })
</script>


Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
DropDownList
Asked by
Season
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or