1

I'm using ASP.net custom validator to validate if mail exists in database or not so I have a server side bool function

public void isUnique(object source, ServerValidateEventArgs args)
{
    args.IsValid = Formatters.FormatUser.AlternateMailUnique(_txtAlternateEmail.Text);    
}

and a custom validator which validates the textbox _txtAlternateEmail

<asp:TextBox ID="_txtAlternateEmail" runat="server" onkeypress="typetext();" onmouseout="textclear();"></asp:TextBox>
                        <asp:CustomValidator runat="server" id="cusCustom" controltovalidate="_txtAlternateEmail"  OnServerValidate="isUnique"  errormessage="Mail already exists" ValidationGroup="savechanges"/>

Meanwhile the validators doesn't act as the .net validators acts, I mean that if the function isUnique returns false the page returns to server and nothing is validated.

2 Answers 2

1

You need to use Page.IsValid property - this property indicates the page validation is succeed or not.

Apart from the isUnique() handler, you need to verify the value of Page.IsValid in button's click handler or other handle in which you want to perform action.

For instance,

protected void Button1_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
    {
      //your actions
     }
  }

PS: Remove JavaScript attribute onmouseout="textclear()" if it clear the content of _txtAlternateEmail.

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

Comments

0

It looks like you didnt put the server validation in your markup. Try adding:

OnServerValidate="isUnique"

4 Comments

@Islam Have you set a breakpoint to make sure its being hit and AlternateMailUnique is actually returning back what you expect? Setting the args.IsValid shouod be all you need to do to handle it.
yes I did and the args.Isvalid handle what I want but the page do a post back which I don't need it to be done, I want it to be like ant .net validator show the error message and prevent any post back
Oh, I get what you are saying. OK well, on a custom validator OnServerValidate call, it HAS to post back. The code is located on the server. I am not sure what "ant" does, I am not familiar with it.
Thanks man, it seems that I'm going to do hand made validator to solve this issue.I will use the on text change of the textbox and use update panel and trigger to check my server side code.

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.