I have a asp control listbox. And i have to validate this. This is described below:
<div style="float:top; width:300px">
<span>Anrede</span>
<asp:DropDownList id="dropdownListAnrede" runat="server" BorderStyle="Solid"
TabIndex="1" Width="250px" BackColor="White" BorderColor="Silver" BorderWidth="1px" Height="22px">
<asp:ListItem >Bitte auswählen</asp:ListItem>
<asp:ListItem Value="Herr">Herr</asp:ListItem>
<asp:ListItem Value="Frau">Frau</asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidatorAnrede"
**ClientValidationFunction=""** runat="server"
ControlToValidate="dropdownListAnrede" ValidateEmptyText="true" SetFocusOnError="true"
ForeColor="Red" onservervalidate="CustomValidatorName_ServerValidate"> Wählen Sie bitte eine Anrede aus!</asp:CustomValidator>
</div>
I have to validate as if it dont have a value(Herr/frau) the sumission will not take place. and the error message will show the message is written in the text. I have to write a ClientValidationFunction in javascript. but I wonder how?
Add a dummy value as the first item that's essentially no selection:
<asp:DropDownList id="dropdownListAnrede" runat="server" ...>
<asp:ListItem >-Select One-</asp:ListItem>
Get the drop down list, and check if it has a selected value greater than zero (omit the first). This would be the clientvalidationfunction:
function val(sender, e) {
var ddl = document.getElementById("<%= dropdownListAnrede.ClientID %>");
e.IsValid = ddl.selectedIndex > 0;
}