[英]validate that an email address contains “@” and “.”
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代码”和“结构图”的例子吗?
I suspect you'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 start of validation - but hopefully this will get you going with the particular case you wanted to handle. 它甚至几乎没有验证的开始 - 但希望这将使你了解你想要处理的特定情况。
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 ... 我尝试了很多验证器,但它们不完整,因为电子邮件地址可能非常宽松......
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 '@'. 和'@'。
Use String.indexOf if you aren't allowed to use regexp, and but I would adwise you to not validate the address during input. 如果不允许使用regexp,请使用String.indexOf,但我还是希望您在输入期间不验证地址。
It's better to send an activation email. 发送激活电子邮件会更好。
You can also use the Java Mail API. 您还可以使用Java Mail API。 Specifically, here: 具体来说,这里:
http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/InternetAddress.html#InternetAddress(java.lang.String , boolean) http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/InternetAddress.html#InternetAddress(java.lang.String,boolean )
You can search for the first '@', then check if what you have at the left of the '@' is a valid string (ie 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. 无论如何,我建议使用正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.