在Android中验证电子邮件地址(例如从用户输入字段)的好技术是什么?emailvalidator似乎不可用。还有其他库做这个,包括在Android已经或我必须使用RegExp?
当前回答
你可以这样写一个Kotlin扩展:
fun String.isValidEmail() =
isNotEmpty() && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
然后像这样调用它:
email.isValidEmail()
其他回答
你可以通过oval.jar文件在android中进行任何类型的验证。OVal是一种实用的、可扩展的通用验证框架,适用于任何类型的Java对象。
点击这个链接:http://oval.sourceforge.net/userguide.html
你可以从这里下载:http://oval.sourceforge.net/userguide.html#download
您可以通过在变量中设置标记来使用验证
public class Something{
@NotEmpty //not empty validation
@Email //email validation
@SerializedName("emailAddress")
private String emailAddress;
}
private void checkValidation() {
Something forgotpass.setEmailAddress(LoginActivity.this.dialog_email.getText().toString());
Validator validator = new Validator();
//collect the constraint violations
List<ConstraintViolation> violations = validator.validate(forgotpass);
if(violations.size()>0){
for (ConstraintViolation cv : violations){
if(cv.getMessage().contains("emailAddress")){
dialog_email.setError(ValidationMessage.formattedError(cv.getMessage(), forgotpass));
}
}
}
}
对于正则表达式爱好者来说,我迄今为止发现的最好的(例如与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 ;
}
最简单的电子邮件验证方式。
EditText TF;
public Button checkButton;
public final Pattern EMAIL_ADDRESS_PATTERN = 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}" +
")+"
);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
TF=(EditText) findViewById(R.id.TF);
checkButton=(Button) findViewById(R.id.checkButton);
checkButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String email=TF.getText().toString();
if(checkEmail(email))
Toast.makeText(getApplicationContext(),"Valid Email Addresss", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
}
});
}
private boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}}
试试这段代码..它真的有效。
if (!email
.matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"))
{
Toast.makeText(getApplicationContext(), "Email is invalid",
Toast.LENGTH_LONG).show();
return;
}
这是Android Studio的建议:
public static boolean isEmailValid(String email) {
return !(email == null || TextUtils.isEmpty(email)) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
推荐文章
- 警告: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文件