Validate a DropDown List Using Custom Validator

Standard

To do this Add a custom validator to your drop down like that


<asp:DropDownList ID="_districtDropDownList" runat="server" ></asp:DropDownList>
<asp:CustomValidator ID="_districtCustomValidator" runat="server"  
ErrorMessage="error message" ControlToValidate="_districtDropDownList"  
ClientValidationFunction="SelectTypeValidator">
</asp:CustomValidator> 
<script>

function SelectTypeValidator(sender, args)
{
   if (args.Value == "-1")  
         args.IsValid = false;   
  else args.IsValid = true;
} 
</script>
Advertisements

Custom validation method doesn’t fired if it used with textbox control and its value is empty

Standard

‚ÄčTo avoid using of the ValidatorEnable method to disable required field validator at some cases on textbox control I decided to use custom validator as a required field just like the following:

<asp:CustomValidator ID="_examDateCustomValidator" runat="server" Text="*"
 ClientValidationFunction="CustomValidateDDL" ControlToValidate="_textbox">

And here is the javascript validation method

function CustomValidateDDL(sender, args)
{
  if (args.Value == "")
      args.IsValid = false;
  else
       args.IsValid = true;
}

unfortunatily I found that this method is working well if I compare the value with any other value except empty text