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.
How I can validate this.Below us my current script
JavaScript
x
22
22
1
<script type="text/javascript">
2
$(document).ready(function () {
3
$('#form1').bootstrapValidator({
4
container: '#messages',
5
feedbackIcons: {
6
valid: 'glyphicon glyphicon-ok',
7
invalid: 'glyphicon glyphicon-remove',
8
validating: 'glyphicon glyphicon-refresh'
9
},
10
fields: {
11
<%=ddIndProvince.UniqueID%>:{
12
validators:{
13
notEmpty:{
14
messages:'please select province'
15
}
16
}
17
}
18
}
19
});
20
});
21
</script>
22
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:
JavaScript
1
2
1
<option value="Something is in here">--Select--</option>
2
To:
JavaScript
1
2
1
<option value="">--Select--</option>
2
Do this in your Page_Load method:
JavaScript
1
9
1
protected void Page_Load(object sender, EventArgs e)
2
{
3
/*...*/
4
5
//Insert new Item at Index 0 with text "--Select--" and no value
6
ddIndProvince.Items.Insert(0, new ListItem("--Select--", String.Empty));
7
ddIndProvince.SelectedIndex = 0;
8
}
9