<asp:DropDownList ID="ddlaccesstype" runat="server" ValidationGroup="Chk" Width="171px">
<asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
<asp:ListItem Text="Admin" Value="1"></asp:ListItem>
<asp:ListItem Text="Supervisor" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqFavoriteColor" Text="(Required)" InitialValue="0"
ControlToValidate="ddlaccesstype" runat="server" />
Using javascript
<script type="text/javascript">
function Validate() {
var ddl = document.getElementById('<%=DropDownList1.ClientID%>');
var option = ddl.options[ddl.selectedIndex];
alert(option.value);
if (option.value == 0) {
alert('select value from dropdown');
return false;
}
else {
return true;
}
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate();" />
------------------------
for textbox
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="IsEmpty();return false;" />
<script type="text/ecmascript">
function IsEmpty() {
var name = document.getElementById("<%= TextBox1.ClientID %>").value;
if (name == "") {
alert("Name can't be empty");
name.focus();
}
}
</script>