Is there any way to make a textbox input validation for email addresses in wpf C#? Regex or validation expression or anything that can help, best with code sample and some instructions
-
1Look at the related column on the right. Thousands of answer for this, just search. – Steve Nov 23 '15 at 22:48
-
the reason I asked is cuz the answers on the other similar questions are old. and I thought maybe there is something more easier and more up to date!! – Tinaira Nov 23 '15 at 22:59
4 Answers
On the text_changed event you could pass the value of the textbox to a helper class.
public static class ValidatorExtensions
{
public static bool IsValidEmailAddress(this string s)
{
Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}
Now on the text changed event you can test the input
private void myTextBox_TextChanged(object sender, EventArgs e)
{
bool result = ValidatorExtensions.IsValidEmailAddress( myTextBox.Text );
}

- 4,002
- 2
- 19
- 21
-
can you please tell me what to write on the text_changes event cuz I'm a bit confused right now – Tinaira Nov 23 '15 at 23:22
-
-
I did it with if statement, it worked also, and with lostfocus event... Anyway, thanks a lot.... Is it possible to describe the regex so I can understand it – Tinaira Nov 25 '15 at 08:38
-
1Please don't use short/incorrect regular expressions like this, otherwise people like myself find that web sites declare our e-mail addresses to be invalid, when they're not. Instead, please consider this answer https://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address – Jono Aug 25 '17 at 15:25
There are several ways to check if the email address is valid
About System.Net.Mail.MailAddress
About Regex Expression
static void Main(string[] args)
{
var validMail = "validMail@gmail.com";
var invalidMail = "123";
Console.WriteLine("IsValidMailAddress1 Test");
Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", validMail, IsValidMailAddress1(validMail)));
Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", invalidMail, IsValidMailAddress1(invalidMail)));
Console.WriteLine("IsValidMailAddress2 Test");
Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", validMail, IsValidMailAddress2(validMail)));
Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", invalidMail, IsValidMailAddress2(invalidMail)));
}
static bool IsValidMailAddress1(string mail)
{
try
{
System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(mail);
return true;
}
catch
{
return false;
}
}
static bool IsValidMailAddress2(string mailAddress)
{
return Regex.IsMatch(mailAddress, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
}

- 151
- 1
- 4
-
-
Actually it's very similar. You might create method in your helper class . You can call from anywhere you want. – Fatih Sert Nov 23 '15 at 23:56
This is the best one I found:
Regex.IsMatch(emailAddress, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)

- 131
- 8
Actually, a great number of things have to be considered when validating email addresses:
Nearly all Regex expressions found on the Internet are not 100% correct
Even if the Regex is doing a 100% perfect job, the email client like Outlook or email server like Exchange might not accept the exotic, but valid email address.
Just one example of a strange, but valid email address:
"very.(),:;<>[]\".VERY.\"very@\ \"very\".unusual"@strange.example.com
which is of the type:
John.Doe@example.com
But actually, RFC 5322: Internet Message Format: Address Specification specifies that en email address looks like this:
John Doe<John.Doe@example.com>
The first "John Doe" is the display name.
If you add display name validation, it becomes rather complicated. But even without display name, most regex fail to recognise the following email address as valid: "John@Doe"@example.com
Therefore the recommendation is just to warn the user if an email address looks strange, but to let the user decide if he made a typo or if he actually wants to enter that email address.
Additionally to validation, it might be helpful when the user can key in only legal characters.
Read my article CodeProject: Email Address Validation Explained, it comes with the code of a fully implemented and tested WPF Email Textbox.

- 3,052
- 2
- 30
- 42