How to use a .NET CustomValidator control with server-side validation

Question

How can I use a .NET CustomValidator control with server-side validation?

Background

Validation using .NET CustomValidator controls cannot be executed client-side using JavaScript because it involves calling methods or other objects only available server side (code-behind).

If you want to use a .NET CustomValidator control with client-side validation code, see How can I use a .NET CustomValidator control in my WorkflowGen web form?.

Notes

  • In order to apply your custom validation to the WFGENPage validation group, you should have at least one field specified in the FORM_FIELD_REQUIRED parameter (see My .NET validator does not trigger the validation error message), otherwise WFGENPage will not be instantiated and your validation result will not affect your WorkflowGen page submission.

  • When using a .NET CustomValidator control and validation that occurs server-side, the error cannot be combined with the WFGENPage validation groups submission validator report dialog box since WorkflowGen handles its default validation at client-side before the form is posted back to the server. You will need to either display the error message at the position of your CustomValidator control on your webform (set your CustomValidator Display attribute to Dynamic) or manually trigger a JavaScript alert dialog box when the page is reloaded.

  • The ControlToValidate attribute may not be necessary. You can specify which fields you would like to validate in your server-side validator method and define the final validation result args.IsValid to either be true or false. If you do specify a value for the ControlToValidate attribute with a control ID (normally applied to controls such as a TextBox, DropDownList, etc., common text-based .NET controls) you can perform the validation by checking args.Value instead of using the control ID in your server-side validator method.

Examples

Web form:

<asp:TextBox runat="server"></asp:TextBox>
ControlToValidate="REQ_ AMOUNT"
Display="Dynamic" 
ErrorMessage="The amount is invalid"
OnServerValidate="MyRangeValidator"
ValidateEmptyText="True"
ValidationGroup="WFGENPage"
EnableClientScript="False"
SetFocusOnError="True">
</asp:CustomValidator>

In code-behind (C#):

protected void MyRangeValidator(object source, ServerValidateEventArgs args)
 {
 if (args.Value != string.Empty
  args.Value matches your validation criteria)
 { args.IsValid = false; }
 else
 { args.IsValid = false; }
 }

Test environment:

  • The EFORMASPX application parameter FORM_FIELD_REQUIRED must have at least one control ID specified. You can simply specify the field to be validated. In the above example, we use REQ_AMOUNT.

Test pattern:

  • If you click Submit without entering a value for REQ_AMOUNT, the web form will perform a post back and return with the error message next to the REQ_AMOUNT textbox.

  • If you click Submit with a valid value entered in REQ_AMOUNT, the web form will submit back to WorkflowGen.