问题
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
回答1:
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 );
}
回答2:
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})+)$");
}
回答3:
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)
回答4:
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.
来源:https://stackoverflow.com/questions/33882173/email-address-input-validation