电子邮件验证的正则表达式 [英] Regular expression for email validation
问题描述
你好,
我有一个正则表达式来验证电子邮件地址。这是....
<前lang =c#> 字符串 pattern = ^ [^ @] + @([ - \\w] + \\。)+ [A- ZA-Z] {2,4} $跨度>;
System.Text.RegularExpressions.Match match = Regex.Match(txtEmailChecking.Text.Trim(),pattern,RegexOptions.None);
if (match.Success)
MessageBox.Show( 成功, 检查);
else
MessageBox.Show( 失败, 检查);
但是这个表达式允许在电子邮件地址中输入大写字母。我该怎么限制呢?
除此之外,这个表达式允许多个@。我该如何解决这个问题?
感谢您的支持
问候
Sebastian
System.Text.RegularExpressions.Match match = Regex.Match(txtEmailChecking.Text。 Trim(),pattern,RegexOptions.None);
if (match.Success)
MessageBox.Show( 成功, 检查);
else
MessageBox.Show( 失败, 检查);
但是这个表达式允许在电子邮件地址写大写字母。我怎么能限制它呢?
除此之外,这个表达式允许mor不止一个@。我该如何解决这个问题?
感谢您的支持
问候
Sebastian
如果是 Windows应用程序
设置Textbox的属性
CharacterCasing = Lower
它会自动将大写字母转换为小写字母。
string pattern = ^([0 -9a-ZA-Z]([ - 。\w] * [0-9A-ZA-Z])* @([0-9A-ZA-Z] [ - \w] * [0-9a- zA-Z] \。)+ [a-zA-Z] {2,3})
;
并在文本框的离开事件时验证它。在你写作的时候会允许像'@'这样的错误是两次,但是当你离开文本框时会出错。
如果你不想要在按键事件发生时'@'然后在按键事件中,如果第二次使用它,你应该处理'@'keychar。您可以使用以下函数检查字符串中是否已存在'@'
str。包含( @)
快乐编码!
:)
Hello,
I have a regular expression to validate email address. Here it is....
string pattern = "^[^@]+@([-\\w]+\\.)+[A-Za-z]{2,4}$";
System.Text.RegularExpressions.Match match = Regex.Match(txtEmailChecking.Text.Trim(), pattern, RegexOptions.None);
if (match.Success)
MessageBox.Show("Success", "Checking");
else
MessageBox.Show("Fail", "Checking");
But this expression will allow capital letters in email address. How can I restrict that?
Besides that , this expression allows more than one @. How can I fix this?
Thanks for your support
Regards
Sebastian
"; System.Text.RegularExpressions.Match match = Regex.Match(txtEmailChecking.Text.Trim(), pattern, RegexOptions.None); if (match.Success) MessageBox.Show("Success", "Checking"); else MessageBox.Show("Fail", "Checking");
But this expression will allow capital letters in email address. How can I restrict that?
Besides that , this expression allows more than one @. How can I fix this?
Thanks for your support
Regards
Sebastian
if it is Windows Application
set property of Textbox
CharacterCasing = Lower
it will automatically convert uppercase letters to lowercase.
string pattern="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})
";
and verify it when textbox's Leave event fire. at a time when you are writing it will allow mistakes like '@' is two time but when you will leave textbox it will give error.
if you do not want '@' at time of keypress event then on key press event you should handle '@' keychar if it's used second time. you can check if '@' is already exist in string using below function
str.Contains("@")
Happy Coding!
:)
这篇关于电子邮件验证的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!