java 验证电子邮件地址是否包含“@”和“.”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5955739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:38:51  来源:igfitidea点击:

validate that an email address contains "@" and "."

javaemailvalidationemail-address

提问by r.r

i need to validate that an inserted email address contains "@" and "." without a regular expression. Can somebody to give me "java code" and "structure chart" examples please?

我需要验证插入的电子邮件地址是否包含“@”和“。” 没有正则表达式。有人可以给我“java代码”和“结构图”的例子吗?

回答by Jon Skeet

I suspectyou're after something like:

怀疑你在追求类似的东西:

if (!address.contains("@") || !address.contains("."))
{
    // Handle bad address
}

EDIT: This is far from a complete validation, of course. It's barely even the startof validation - but hopefully this will get you going with the particular case you wanted to handle.

编辑:当然,这远非完整的验证。这甚至只是验证的开始- 但希望这能让您处理您想要处理的特定情况。

回答by Jigar Joshi

回答by Bhushan

int dot = address.indexOf('.');
int at = address.indexOf('@', dot + 1);

if(dot == -1 || at == -1 || address.length() == 2) {
  // handle bad address
}

This is not complete solution. You will have to check for multiple occurances of @ and address with only '.' and '@'.

这不是完整的解决方案。您将不得不检查多次出现的@ 和仅带有“.”的地址。和 '@'。

回答by VirtualTroll

From my personal experience, the only was to validate an email address is to send a email with a validation link or code. I tried many of the validator but they are not complete because the email addresses can be very loose ...

根据我的个人经验,验证电子邮件地址的唯一方法是发送带有验证链接或代码的电子邮件。我尝试了许多验证器,但它们并不完整,因为电子邮件地址可能非常松散......

回答by Nathan Ryan

回答by Santiago V.

You can search for the first '@', then check if what you have at the left of the '@' is a valid string (i.e. it doesn't have spaces or whatever). After that you should search in the right side for a '.', and check both strings, for a valid domain.

您可以搜索第一个“@”,然后检查“@”左侧的内容是否是有效字符串(即它没有空格或其他任何内容)。之后,您应该在右侧搜索“.”,并检查两个字符串以获取有效域。

This is a pretty weak test. Anyway I recommend using regular expressions.

这是一个相当弱的测试。无论如何,我建议使用正则表达式。

回答by Kaj

Use String.indexOf if you aren't allowed to use regexp, and but I would adwise you to not validate the address during input.

如果不允许使用正则表达式,请使用 String.indexOf,但我建议您不要在输入期间验证地址。

It's better to send an activation email.

最好发送激活电子邮件。