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

**URL:** <https://discuss.workflowgen.com/t/how-to-use-a-net-customvalidator-control-with-server-side-validation/194>\
**Category:** Visual Studio Web Forms\
**Created:** [November 3, 2009, 5:07am UTC](https://discuss.workflowgen.com/t/how-to-use-a-net-customvalidator-control-with-server-side-validation/194 "2009-11-03T05:07:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wfg-admin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/wfg-admin/32/714_2.png) [@wfg-admin](https://discuss.workflowgen.com/u/wfg-admin)\
**Post date:** [November 3, 2009, 5:07am UTC](https://discuss.workflowgen.com/t/how-to-use-a-net-customvalidator-control-with-server-side-validation/194/1 "2009-11-03T05:07:00Z")

</div>

**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?](https://discuss.workflowgen.com/t/how-can-i-use-a-net-customvalidator-control-in-my-workflowgen-web-form/174).

**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](https://discuss.workflowgen.com/t/my-net-validator-does-not-trigger-the-validation-error-message/354)), 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:

```auto
<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#):

```auto
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.
