7

I've a CustomValidator and I defined every possible parameter of it:

<asp:CustomValidator ID="custom" runat="server" Text="*" ErrorMessage="This email address is already registered" ControlToValidate="txtEmail" OnServerValidate="isExist" Display="None" ValidationGroup="valRegister"></asp:CustomValidator>

PS: I've a RequiredFieldValidator for same textbox and I dont want to check empty value.

Here are other objects of the form:

<div class="row"><asp:Label runat="server" Text="Email" AssociatedControlID="txtEmail"></asp:Label><asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter your email" Text="*" ControlToValidate="txtEmail"></asp:RequiredFieldValidator><asp:TextBox ID="txtEmail" runat="server" CssClass="inpBox"></asp:TextBox></div>

<asp:Button runat="server" Text="Register" CssClass="btn" OnClick="register_member" CausesValidation="true" ValidationGroup="valRegister" />

<asp:ValidationSummary ID="validationSummary" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="valRegister" />

protected void isExist(object sender, ServerValidateEventArgs args){
if (cre.member.isExist(args.Value)){
    args.IsValid = false;
} else {
    args.IsValid = true;
}

}

When I put an email already exist in the db table * appears on the form, but the error message doesnt show up. I tried all display options for custom error but no luck.

0

2 Answers 2

6

I took the code exactly as in your question.

Changing Display="None" to Display="Dynamic" in the asp:CustomValidator causes the asterisk to appear.

Changing ShowSummary="false" to ShowSummary="true" in the asp:ValidationSummary causes the error message to appear in the summary.

Sign up to request clarification or add additional context in comments.

3 Comments

I realized something with your answer, OnServerValidate only works if ValidationSummary > ShowSummary is "true". ShowMessageBox works on client side and because of that OnServerValidate doesnt work. I solved my problem by adding an ajax call to an aspx page to check entered email address and then I used ClientValidationFunction to call that js function.
Aha! Sorry, I didn't realize it was the messagebox validation error you were after. Yes, if you want the client-side messagebox but require the server's involvement (e.g. to check for an email address in a database) then you will have to do something like this.
It works fine in normal case, but as soon I put the form controls inside an update panel, all validators get their error messages displayed fine (within the validation summary) EXCEPT the custom validator one. Still don't understand why.
2

Changing the Display to "Dynamic" or anything doesn't really do anything if the server is not handling the validation manually, especially when using <asp:CustomValidator. Even a ValidationGroup with or without a ValidationSummary does nothing.

Always force a validation on the server before allowing the user to exit the form/gridview/etc.

ie

            ...your form here...
             <tr>
                <td colspan="3" style="text-align: center" valign="top">
                    <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="Submit_Click" CausesValidation="true"  />
                    <asp:Button ID="ButtonCancel" runat="server" Text="Cancel" OnClick="Cancel_Click" CausesValidation="false"  />
                </td>
            </tr>
        </table>
    </asp:Panel>

...
    protected void Submit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
           //processing done after a successful submit here!
        }
    }

The Page.Validate() will force the validation controls to check and display your error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.