Click here to Skip to main content
15,891,719 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Anybody help me for validating email address for space.
Posted
Updated 31-May-18 11:20am
Comments
Prasad_Kulkarni 12-Mar-13 3:24am    
..what does that mean Gaurav? Please elaborate possibly with some code snippets which you have tried so far..
Gaurav Jadhav 12-Mar-13 3:26am    
i don't want space in my email address. For that i want validation..
Gaurav Jadhav 12-Mar-13 3:38am    
prasad,
i want the coding on textchanged event...
pls help me.
Gaurav Jadhav 12-Mar-13 3:51am    
i hd never tried yet....
i want code on textchnged event

You can have a look on MailAddress Class[^]
C#
public bool IsValid(string emailaddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

This is something where you don't need to understand any other's regx code.

Also have a look on this example where you can get an example with source for email validation
c# regex for email address[^]

An alternative:
C#
public bool IsValidEmailAddress(string s)
        {
            if (string.IsNullOrEmpty(s))
                return false;
            else
            {
                var regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                return regex.IsMatch(s) && !s.EndsWith(".");
            }
        }
 
Share this answer
 
Use RegularExpressionValidator .

C#
<asp:regularexpressionvalidator id="regexEmailValid" runat="server" validationexpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" controltovalidate="tbEmail" errormessage="Invalid Email Format" xmlns:asp="#unknown"></asp:regularexpressionvalidator> 
 
Share this answer
 
use regular expression.
C#
[Regex(regular expression)]
            public string Email { get; set; }
 
Share this answer
 

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