Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I am trying to use CustomValidator for validating email.I tried using both server and client side validations.
But, my code is not getting hit.

Please guide.Thanks.

What I have tried:

C#
<pre>asp:TableRow>
            <asp:TableCell>Email</asp:TableCell>
             <asp:TableCell>
                <asp:TextBox runat="server" ID="email"  CausesValidation="true"></asp:TextBox>
               <asp:RequiredFieldValidator runat="server" ID="emailvalidator" ControlToValidate="email"  ErrorMessage="Email required" style="color:red"></asp:RequiredFieldValidator>
               <asp:CustomValidator runat="server"  ControlToValidate="email" ErrorMessage="Valid Email required" ValidateEmptyText="True" ClientValidationFunction="validatemail" EnableClientScript="true" OnServerValidate="email_serverValidation"></asp:CustomValidator>
            </asp:TableCell>
        </asp:TableRow>



JavaScript
<pre>    <script type="text/javascript">
        function validatemail(sender,args)
        {
            debugger;
            var val = args.Value;
            var regex = /^[a-zA-Z0-9]{1-10}@[a-zA-Z]{1-10}.(com|org)$/;
            if(regex.test(val))
            {
                args.IsValid = true;
            }
        }
    </script>


C#
protected void email_serverValidation(object sender, ServerValidateEventArgs e)
       {

           System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^([a-zA-Z0-9]+)@[a-zA-Z]{1-10}.(com|org)$");
           if (r.IsMatch(email.Text))
           {
               e.IsValid = true;
           }

       }
Posted
Comments
[no name] 3-Jun-17 9:27am    
What issue you are facing here?
Shivangi Rawat 3-Jun-17 9:48am    
The code is not getting hit.
[no name] 3-Jun-17 10:05am    
Which code is not getting hit?
Shivangi Rawat 3-Jun-17 10:08am    
Neither the function in onservervalidate nor in ClientValidationFunction
[no name] 3-Jun-17 10:21am    
I tried and to not see any issue in your code. How do you validating the code. if you want to validate the textbox content it should happen some kind of postback like on button click or text change event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900