在Android中验证电子邮件地址(例如从用户输入字段)的好技术是什么?emailvalidator似乎不可用。还有其他库做这个,包括在Android已经或我必须使用RegExp?
当前回答
我知道已经太迟了,但我还是会给你我的答案。
我使用这行代码来检查输入的电子邮件格式:
!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 Studio的建议:
public static boolean isEmailValid(String email) {
return !(email == null || TextUtils.isEmpty(email)) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
我们现在有一个简单的电子邮件模式匹配器。
Java:
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
芬兰湾的科特林功能:
private fun isValidEmail(email: String): Boolean {
return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches()
}
芬兰湾的科特林扩展:
fun String.isValidEmail() =
!TextUtils.isEmpty(this) && Patterns.EMAIL_ADDRESS.matcher(this).matches()
Following是我用的。然而,它包含额外的字符比正常的电子邮件,但这是对我的要求。
public boolean isValidEmail(String inputString) {
String s ="^((?!.*?\.\.)[A-Za-z0-9\.\!\#\$\%\&\'*\+\-\/\=\?\^_`\{\|\}\~]+@[A-Za-z0-9]+[A-Za-z0-9\-\.]+\.[A-Za-z0-9\-\.]+[A-Za-z0-9]+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputString);
return matcher.matches();
}
这个问题的答案是:- 要求用给定的点验证电子邮件地址
解释,
(? !)。* ? . .)“Negative Lookhead”否定2个连续的点。 [A-Za-z0-9 .!#\$\%\&\'*+-/\=\?\^_ '{\|}\~]+至少一个 角色定义。(“\”用于转义)。 可能有一个。 [A-Za-z0-9]+然后至少定义一个字符。 [A-Za-z0-9 -。*零或任何已定义字符的重复。 [A-Za-z0-9]+点后至少一个字符。
使用简单的一行代码进行电子邮件验证
public static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
使用像…
if (!isValidEmail(yourEdittext.getText().toString()) {
Toast.makeText(context, "your email is not valid", 2000).show();
}
我知道已经太迟了,但我还是会给你我的答案。
我使用这行代码来检查输入的电子邮件格式:
!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。都对我虚情假意。
推荐文章
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败