C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » Windows »

Email Validation


Posted Date: 04 Nov 2009      Posted By: satheesh      Member Level: Silver     Points: 1   Responses: 3



Hi all,

email format validation for the textbox in C# windows forms
send me the link or code





Responses

Author: Lalji    04 Nov 2009Member Level: DiamondRating: 3 out of 53 out of 53 out of 5     Points: 3

you can try this code sample

private void textBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (textBox1.Text.Length > 0)
{
if (!rEMail.IsMatch(textBox1.Text))
{
MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.SelectAll();
e.Cancel = true;
}
}
}



Author: vipul    04 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

hi,
for that you can do this way
First go to the textbox property in that event and select "textBox1_Validating" event and in that you ca nwrite this way

using System.Text.RegularExpressions;
private void textBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (textBox1.Text.Length > 0)
{
if (!rEMail.IsMatch(textBox1.Text))
{
MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.SelectAll();
e.Cancel = true;
}
}
}


Please Rate This Answer If They Helpful

Thanks & Regards
Patel Vipul



Author: yugandherReddy    10 Nov 2009Member Level: BronzeRating: 2 out of 52 out of 5     Points: 2

use this code..

public static class TestEmail
{
/// <summary>
/// Regular expression, which is used to validate an E-Mail address.
/// </summary>
public const string MatchEmailPattern =
@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

/// <summary>
/// Checks whether the given Email-Parameter is a valid E-Mail address.
/// </summary>
/// <param name="email">Parameter-string that contains an E-Mail address.</param>
/// <returns>True, when Parameter-string is not null and
/// contains a valid E-Mail address;
/// otherwise false.</returns>
public static bool IsEmail(string email)
{
if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
else return false;
}
}



Post Reply
You must Sign In to post a response.
Next : How to add my database to my Setup
Previous : To make checkbox checked
Return to Discussion Forum
Post New Message
Category: Windows

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use