[英]Android email validation with regular expression not working
I have a regular expression to validate an email and when I run it, it constantly tells me that its an invalid email address but the email is correct 我有一个用于验证电子邮件的正则表达式,当我运行它时,它会不断告诉我该电子邮件地址无效,但该电子邮件是正确的
email = (EditText) findViewById(R.id.email);
final String Email = email.getText().toString();
if (!Email.matches("^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$"))
{
email.requestFocus();
email.setError("INVALID EMAIL ADDRESS");
}
Does anyone know it would give me the error even though it is correct? 有谁知道它会给我错误,即使它是正确的?
try this instead. 试试这个吧。 this should remove any hidden spaces/characters after the email.
这应该删除电子邮件后的所有隐藏空格/字符。
Email.matches
("([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}).*"))
according to http://developer.android.com/reference/java/lang/String.html 根据http://developer.android.com/reference/java/lang/String.html
public boolean matches (String regularExpression)
公共布尔匹配(字符串regularExpression)
Added in API level 1 Tests whether this string matches the given regularExpression.
在API级别1中添加测试该字符串是否与给定的regularExpression相匹配。 This method returns true only if the regular expression matches the entire input string.
仅当正则表达式匹配整个输入字符串时,此方法才返回true。 A common mistake is to assume that this method behaves like contains(CharSequence);
一个常见的错误是假定此方法的行为类似于contains(CharSequence);。 if you want to match anywhere within the input string, you need to add .* to the beginning and end of your regular expression.
如果要匹配输入字符串中的任何位置,则需要在正则表达式的开头和结尾添加。*。 See matches(String, CharSequence).
参见matchs(String,CharSequence)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.