Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Friends;

I have Small Problem .i m Validate the Email id with (-).
my code Not working

My Code below :

C#
if (!string.IsNullOrEmpty(txtEmailID.Text.Trim()))
                {
                    // Email Validation using regular expressions
                    //string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z|0-9|]*[a-z|0-9|][a-z|0-9|]*\.([a-z]" +
                    // @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
                    string pattern = @"/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i";


                    System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(txtEmailID.Text.Trim(), pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    if (!match.Success)
                    {
                        MessageBox.Show("Please enter a valid Email ID", Common.MSGBOX_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtEmailID.Focus();
                        return false;
                    }
                }

Plz help Me

Regards
sathya.
Posted
Comments
Kiirrii 5-Apr-12 4:00am    
why dont u use validating controls ..
SathyaRaju 5-Apr-12 4:04am    
i m using the windows application

try the below pattern.

string strPattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
 
Share this answer
 
v2
Hi ,
try this one
XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
         ErrorMessage=" your Error message" ControlToValidate="TextBox1"
         Display="Dynamic" ForeColor="#FF3300" SetFocusOnError="True"
         ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>



you can also use it code behind
C#
RegularExpressionValidator reg = new RegularExpressionValidator();
reg.ID = "RegularExpressionValidator1";
reg.ControlToValidate = "TextBox1";
reg.Display = ValidatorDisplay.Dynamic;
reg.ErrorMessage = "your Error message";
reg.SetFocusOnError = true;
reg.ValidationExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
form1.Controls.Add(reg);
reg.Validate();



Best Regards
M.Mitwalli
 
Share this answer
 
v2
VB
<asp:Label id="lblEmail"
Text="Email ID" runat="server" />

<asp:TextBox ID="txtEmail"
MaxLength="50" runat="server" />
<asp:RequiredFieldValidator ID="txtEmailValidator"
ControlToValidate="txtEmail"
Text="*" ErrorMessage="Email is Missing"
runat="server" />

<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="txtEmail"
Text="Invalid Email Address"
ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)
|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" runat="server" />
 
Share this answer
 
Try this Regex:
([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})

It is taken directly from the Expresso Expression Library.
Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
SathyaRaju 5-Apr-12 4:17am    
i get the error.......like too many
OriginalGriff 5-Apr-12 4:24am    
Sorry? Please try to explain in a little more detail.
SathyaRaju 5-Apr-12 4:28am    
too many parameter not correct format....
OriginalGriff 5-Apr-12 4:32am    
Did you insert it as the pattern string?
If so, show your code.
C#
pattern = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"


if (Regex.IsMatch(TextBox1.Text , pattern))
{
    Label2.Text = "Valid Email address ";
}


Full source code.. Asp.Net email Validation

Niva
 
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