Hi, I have a custom validator (.net 3.5) that checks if four dropdown lists in my form have repeated values. It works on the server-side but I would like to add a client-side function to go with it. I have no knowledge of JavaScript. Could you help? Mant thanks.
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage = "Same related document was entered more than once" OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"></asp:CustomValidator>
Protected Sub dropDownValidation_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = Not haveSameValue(DropDownList9.SelectedValue, DropDownList12.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList15.SelectedValue, DropDownList18.SelectedValue)
End Sub
Protected Function haveSameValue(ByVal first As String, ByVal second As String) As Boolean
If first <> "" And second <> "" AndAlso first.Equals(second) Then
Return first.Equals(second)
End If
End Function
UPDATE: The following JavaScript code works ok as it checks if there are duplicate values in the dropdown lists. However how can I link this to my custom validator and eliminate the alert message. As it stands now the page gets submitted. Thanks.
function dropDownValidation_ClientValidate() {
var strValue1 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList1');
var strValue2 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList2');
var strValue3 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList3');
var strValue4 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList4');
var result = haveSameValue(strValue1.value, strValue2.value) &&
haveSameValue(strValue1.value, strValue3.value) &&
haveSameValue(strValue1.value, strValue4.value) &&
haveSameValue(strValue2.value, strValue3.value) &&
haveSameValue(strValue2.value, strValue4.value) &&
haveSameValue(strValue3.value, strValue4.value);
return result;
}
function haveSameValue(ddlValue1, ddlValue2) {
if (ddlValue1 != null && ddlValue1 != '' && ddlValue2 != null && ddlValue2 != '' && ddlValue1 == ddlValue2){
alert("Related documents contain duplicate values");
}
}