在Android中验证电子邮件地址(例如从用户输入字段)的好技术是什么?emailvalidator似乎不可用。还有其他库做这个,包括在Android已经或我必须使用RegExp?
当前回答
对于正则表达式爱好者来说,我迄今为止发现的最好的(例如与RFC 822一致)电子邮件模式如下(在PHP提供过滤器之前)。我想对于那些使用API < 8的人来说,将其翻译成Java很容易:
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 ;
}
其他回答
自API 8 (android 2.2)以来,有一个模式:android.util. patterns . email_address http://developer.android.com/reference/android/util/Patterns.html
所以你可以用它来验证yourEmailString:
private boolean isValidEmail(String email) {
Pattern pattern = Patterns.EMAIL_ADDRESS;
return pattern.matcher(email).matches();
}
如果邮件有效,则返回true
乌利希期刊指南: 这个模式的源代码是:
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}" +
")+"
);
参考网址:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/util/Patterns.java
所以你可以自己构建它来兼容API < 8。
试试这个简单的方法,它不能接受以数字开头的电子邮件地址:
boolean checkEmailCorrect(String Email) {
if(signupEmail.length() == 0) {
return false;
}
String pttn = "^\\D.+@.+\\.[a-z]+";
Pattern p = Pattern.compile(pttn);
Matcher m = p.matcher(Email);
if(m.matches()) {
return true;
}
return false;
}
我知道已经太迟了,但我还是会给你我的答案。
我使用这行代码来检查输入的电子邮件格式:
!TextUtils.isEmpty(getEmail) && android.util.Patterns.EMAIL_ADDRESS.matcher(getEmail).matches();
问题是,它只检查格式而不是拼写。
当我输入@gmal.com漏了I, @yaho.com又漏了一个o。 返回true。因为它满足电子邮件格式的条件。
我所做的是,我使用上面的代码。因为它会返回true如果 如果用户输入@gmail.com ONLY,开头没有文本。
格式检查
如果我输入这个电子邮件,它会给我:true,但拼写是错误的。在 我的textInputLayout错误
电子邮件地址@yahoo.com, @gmail.com, @outlook.com检查器
//CHECK EMAIL
public boolean checkEmailValidity(AppCompatEditText emailFormat){
String getEmail = emailFormat.getText().toString();
boolean getEnd;
//CHECK STARTING STRING IF THE USER
//entered @gmail.com / @yahoo.com / @outlook.com only
boolean getResult = !TextUtils.isEmpty(getEmail) && android.util.Patterns.EMAIL_ADDRESS.matcher(getEmail).matches();
//CHECK THE EMAIL EXTENSION IF IT ENDS CORRECTLY
if (getEmail.endsWith("@gmail.com") || getEmail.endsWith("@yahoo.com") || getEmail.endsWith("@outlook.com")){
getEnd = true;
}else {
getEnd = false;
}
//TEST THE START AND END
return (getResult && getEnd);
}
返回:假
返回:真
XML:
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextEmailAddress"
android:inputType="textEmailAddress|textWebEmailAddress"
android:cursorVisible="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:maxLength="50"
android:theme="@style/EditTextCustom"/>
注意:我试图从EditText中获取值,并使用split对它甚至StringTokenizer。都对我虚情假意。
使用android:inputType="textEmailAddress"如下:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:inputType="textEmailAddress"
android:id="@+id/email"
/>
and:
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
.matches();
}
在要验证电子邮件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;
}
}
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件