Thursday 4 September 2008

CustomValidator does not fire

Recently, I needed to ensure that a user fill in one of two TextBox controls on a form I was designing. I couldn't use two RequiredFieldValidator controls as that would mandate that both fields be completed. I therefore created a CustomValidator control and added it to the page. It did not work. There was a flaw in my thinking. The control did not ever fire when needed (ie. when both fields are blank), because the control does not, by default, validate blank fields. Thankfully, there is a way to change this. You can set the ValidateEmptyText to true:

<asp:CustomValidator ID="myValidator" runat="server" ErrorMessage="Error message" OnServerValidate="myValidator_ServerValidate ValidateEmptyText="true"></asp:CustomValidator>

No comments: