在Android中验证电子邮件地址(例如来自用户输入字段)的好方法是什么?org.apache.commons.validator.routines.EmailValidator似乎不可用.有没有其他图书馆这样做已经包含在Android中,或者我是否必须使用RegExp?
我有这个EditText定义:
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textEmailAddress"
android:id="@+id/EmailText"/>
Run Code Online (Sandbox Code Playgroud)
请注意,EditText具有使用电子邮件地址规范定义的inputType.Android是否内置了任何内容来验证电子邮件地址输入类型,还是必须手动完成?它允许我输入无效数据,所以我很好奇它的目的.
谢谢.
我想验证EditText中引入的电子邮件,以及我已经拥有的代码:
final EditText textMessage =(EditText)findViewById(R.id.textMessage);
final TextView text =(TextView)findViewById(R.id.text);
textMessage.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (textMessage.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") && s.length() > 0)
{
text.setText("valid email");
}
else
{
text.setText("invalid email");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Run Code Online (Sandbox Code Playgroud)
问题是当我在"@"之后引入3个字符时,它会显示消息"有效电子邮件",当我在介绍完整的电子邮件时它必须出现.
有什么建议吗?
谢谢你们!
我已经在我的应用程序中实现了电子邮件验证,但问题是应用程序接受超过64个字符的本地域部分,当我写..nnn .. @ gmail.com应用程序也接受这个并且还根据RFC 3696规范#第3节,期间(".")不得用于开始或结束本地部分,也不得出现两个或多个连续期间.
请帮帮我
private boolean isValidEmail(String email) {// validation for email Id
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,255}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
Run Code Online (Sandbox Code Playgroud) 我的电子邮件验证有什么问题??
TextInputLayout useremail_layout = (TextInputLayout) findViewById(R.id.email_layout);
String email = "";
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
if (email.matches(emailPattern)&& email.length()>0) {
useremail_layout.setError("valid address");
} else {
useremail_layout.setError("invalid email");
Email.setError(null);
}
Run Code Online (Sandbox Code Playgroud)