博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我应该如何验证电子邮件地址?
阅读量:3581 次
发布时间:2019-05-20

本文共 3344 字,大约阅读时间需要 11 分钟。

本文翻译自:

What's a good technique for validating an e-mail address (eg from a user input field) in Android? 在Android中验证电子邮件地址(例如来自用户输入字段)的好技术是什么? doesn't seem to be available. 似乎不可用。 Are there any other libraries doing this which are included in Android already or would I have to use RegExp? 是否有其他库已包含在Android中,或者我必须使用RegExp?


#1楼

参考:


#2楼

Since API 8 (android 2.2) there is a pattern: android.util.Patterns.EMAIL_ADDRESS 从API 8(android 2.2)开始,有一种模式:android.util.Patterns.EMAIL_ADDRESS

So you can use it to validate yourEmailString: 因此,您可以使用它来验证yourEmailString:

private boolean isValidEmail(String email) {    Pattern pattern = Patterns.EMAIL_ADDRESS;    return pattern.matcher(email).matches();}

returns true if the email is valid 如果电子邮件有效,则返回true

UPD: This pattern source code is: UPD:此模式源代码是:

public static final Pattern EMAIL_ADDRESS    = Pattern.compile(        "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +        "\\@" +        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +        "(" +            "\\." +            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +        ")+"    );

refer to: 请参阅: :

So you can build it yourself for compatibility with API < 8. 因此,您可以自己构建它以与API <8兼容。


#3楼

email is your email-is. 电子邮件是您的电子邮件。

public boolean validateEmail(String email) {    Pattern pattern;    Matcher matcher;    String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";    pattern = Pattern.compile(EMAIL_PATTERN);    matcher = pattern.matcher(email);    return matcher.matches();    }

#4楼

public boolean isValidEmail(String email){    boolean isValidEmail = false;    String emailExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";    CharSequence inputStr = email;    Pattern pattern = Pattern.compile(emailExpression, Pattern.CASE_INSENSITIVE);    Matcher matcher = pattern.matcher(inputStr);    if (matcher.matches())    {        isValidEmail = true;    }    return isValidEmail;}

#5楼

For regex lovers, the very best (eg consistant with RFC 822) email's pattern I ever found since now is the following (before PHP supplied filters). 对于正则表达式爱好者来说,自从现在以来我发现最好的电子邮件模式(例如,与RFC 822一致)如下(在PHP提供的过滤器之前)。 I guess it's easy to translate this into Java - for those playing with API < 8 : 我想将其转换为Java很容易-对于使用API​​ <8的用户:

private static function email_regex_pattern() {// Source:  http://www.iamcal.com/publish/articles/php/parsing_email$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';$quoted_pair = '\\x5c[\\x00-\\x7f]';$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";$domain_ref = $atom;$sub_domain = "($domain_ref|$domain_literal)";$word = "($atom|$quoted_string)";$domain = "$sub_domain(\\x2e$sub_domain)*";$local_part = "$word(\\x2e$word)*";$pattern = "!^$local_part\\x40$domain$!";return $pattern ;}

#6楼

Call This Method where you want to validate email ID. 在要验证电子邮件ID的位置调用此方法。

public static boolean isValid(String email){   String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";   CharSequence inputStr = email;   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);   Matcher matcher = pattern.matcher(inputStr);   if (matcher.matches())    {      return true;   }   else{   return false;   }}

转载地址:http://jmlgj.baihongyu.com/

你可能感兴趣的文章
C/C++之struct的小知识
查看>>
温湿度传感器(AM2312)
查看>>
抖音无水印视频下载,不用借助第三方网站【详细教程】
查看>>
推荐几款好用的Chrome插件,码农的利器。
查看>>
sql 数据库的优化【经验贴】
查看>>
一篇认识 Zookeeper
查看>>
一篇认识kafka
查看>>
Kafka 实战
查看>>
一篇认识 Elasticsearch
查看>>
爬虫篇——腾讯新闻的详细采集过程(列表新闻和新闻内容)
查看>>
NIO 服务器端不阻塞的一个Bug解决
查看>>
DM数据库的安装部署和卸载
查看>>
DM8数据库体系结构
查看>>
DM模式对象的基本操作
查看>>
事件和几个JavaScript实例
查看>>
jQuery基本介绍(和js关系)
查看>>
jQuery选择器和API方法介绍
查看>>
汇编预习三周速成85分之寄存器
查看>>
jQuery对ajax的封装部分详解和案例
查看>>
hibernate简单介绍
查看>>