如何使文本框仅输入电子邮件地址 [英] How do I make the textbox to enter an email address only

查看:86
本文介绍了如何使文本框仅输入电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我输入第一个字符,消息框会立即显示无效的电子邮件



我尝试了什么:



if(textBox3.Text.Contains(@))

{

if(textBox3.Text.Contains(。 com))

{

MessageBox.Show(有效电子邮件);

}

} < br $>
else

{

MessageBox.Show(无效的电子邮件);

}

once i enter the first character the message box immediately shows invalid email

What I have tried:

if (textBox3.Text.Contains("@"))
{
if (textBox3.Text.Contains(".com"))
{
MessageBox.Show("Valid Email");
}
}
else
{
MessageBox.Show("Invalid Email");
}

推荐答案

如何:验证字符串是否为有效的电子邮件格式| Microsoft Docs [ ^ ]


问题是它是对的:无论你输入什么,都不会是有效的电子邮件。

想想看:您的电子邮件是abc@def.ghi

键入a无效时。

当你添加'b'时它仍然无效。

如果只有在'。'之后输入至少两个字符才有效!

abc@def.gh在技术上是一封有效的电子邮件,虽然它可能不会有用。



你不能检查有效的电子邮件,直到用户已完成输入:所以要么在他离开文本框时进行检查并显示一个消息框(这就是验证事件 [ ^ ]用于),或者使用文本框附近的标签来显示无效的电子邮件地址。



但请注意电子邮件地址可能比你想象的要复杂得多:这会通过你的验证但不是有效的:a.com @而这些不会:abc@talktalk.net,abc.def.co.uk ,abc@usa.gov。



尝试使用正则表达式: regex for valid email - Google Search [ ^ ] - 它;可能是做OK基本检查的最简单方法ness,但确保它是真实地址的唯一方法是发送一封需要链接的电子邮件。
The problem is that it is right: whatever you type to startwith will not be a valid email.
Think about it: Your email is "abc@def.ghi"
When you type 'a' that isn't valid.
When you add 'b' it still isn't valid.
If only becomes valid when at least two characters have been typed after the '.'!
"abc@def.gh" is technically a valid email, although it probably won't go anywhere useful.

You can't check for a valid email until the user has finished entering it: so either do your check and show a message box when he leaves the textbox (that's what the Validating event[^] is there for), or use a label near the textbox to show "invalid email address" instead.

But do be aware the email addresses can be more complicated than your seem to think: This would pass your validation but not be valid: "a.com@" while these would not: "abc@talktalk.net", "abc.def.co.uk", "abc@usa.gov".

Try using a Regex: regex for valid email - Google Search[^] - it;'s probably the simplest way to do a basic check of "OKness", though the only way to be sure it's a "real address" is to send an email that requires a link to be followed.


引用:

一旦我输入第一个字符,消息框会立即显示无效的电子邮件

once i enter the first character the message box immediately shows invalid email



这就是原则!

电子邮件地址在完成之前无效。

处理此问题的一种方法是使用输入字段中的颜色更改替换消息框,只要地址无效就将其设置为红色。 br $> b $ b

建议:使用RegEx验证电子邮件地址,便于检查。

只需几个有趣的链接来帮助构建和调试RegEx。

以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:
.NET Regex Tester - 正则表达式风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:

Regexper [ ^ ]


That is the principle!
An Email address is not valid until it is completed.
One way to handle this is to replace the message box with a color change in input field, make it red as long as address is not valid.

Advice: use RegEx to validate Email address, it ease the check.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]


这篇关于如何使文本框仅输入电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆