Skip to content
Advertisement

Validate ASP.NET Dropdownlist Control with bootstrap Validator

I have a asp dropdownlist <asp:DropDownList ID="ddIndProvince" data-style="btn-default" CssClass="form-control input" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddIndProvince_SelectedIndexChanged" TabIndex="8"></asp:DropDownList> On Page load event I inserted “–Select–” item at index 0. Now I am facing problem while I am validating it with bootstrap validator. Its returning valid for “–Select–” item. like below image. enter image description here

How I can validate this.Below us my current script

<script type="text/javascript">
    $(document).ready(function () {          
             $('#form1').bootstrapValidator({
                container: '#messages',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {                   
                    <%=ddIndProvince.UniqueID%>:{
                        validators:{
                            notEmpty:{
                                messages:'please select province'
                            }
                        }
                    }                              
                }   
            });
        });         
    </script>

Advertisement

Answer

bootstrapValidator is validating the value attribute of the <option> tag in your html markup.

Your javascript code is correct. What you need to do is make sure, the value attribute is empty:

Change your markup from:

<option value="Something is in here">--Select--</option>

To:

<option value="">--Select--</option>

Do this in your Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
    /*...*/

    //Insert new Item at Index 0 with text "--Select--" and no value
    ddIndProvince.Items.Insert(0, new ListItem("--Select--", String.Empty));
    ddIndProvince.SelectedIndex = 0;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement