Question

I Have The Following JavaScript Function:

function validateLocality(object, args) {
        if (document.getElementById("<%=ddlLocality.ClientID %>").value == "0") {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }

and the following drop down list with validator:

<asp:DropDownList
  ID="ddlLocality"
  runat="server"
  DataSourceID="DataSourceListTowns"
  DataTextField="town_name"
  DataValueField="town_id"
  ToolTip="The Locality Where the User Lives"
  AppendDataBoundItems="True"
  ViewStateMode="Disabled">
<asp:ListItem Value="0">Select Locality</asp:ListItem></asp:DropDownList>
<asp:CustomValidator
  runat="server"
  ControlToValidate="ddlLocality" 
  ErrorMessage="Select Locality"
  ToolTip="Select Locality"
  ID="validateLocality" 
  ClientValidationFunction="validateLocality()">*</asp:CustomValidator>

The Thing Is It is not validating the drop-down at all.

Thanks For Any Help Cause I wrecked My Brain about this

Was it helpful?

Solution

The ClientValidationFunction should carry only the name of the function. It is not expecting to carrry a Javascript expression. Hence your attribute should look like: ClientValidationFunction="validateLocality" note no ().

OTHER TIPS

Your function is taking 2 arguments:

function validateLocality(object, args)

But when you call it, you aren't passing any in..?

ClientValidationFunction="validateLocality()"

Add an alert to your function to make sure it's being called on the submit.

function validateLocality(object, args)
{
     alert("working");

     if (document.getElementById("<%=ddlLocality.ClientID %>").value == "0")
     {
          args.IsValid = false;
     }
     else
     {
          args.IsValid = true;
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top