如何进行电子邮件验证? 我使用以下代码来检查电子邮件的验证。
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A- Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(string);
上面的代码工作正常,但如果我也输入字符串"example@gmail.com.com"
,
我得到的回复为true
。
我需要验证这一点。我怎么验证这个?请帮帮我。
答案 0 :(得分:20)
使用此代码块 将电子邮件传递给此函数,它将返回布尔值true或false
private boolean validEmail(String email) {
Pattern pattern = Patterns.EMAIL_ADDRESS;
return pattern.matcher(email).matches();
}
if (!validEmail(email)) {
Toast.makeText(YourActivity.this,"Enter valid e-mail!",Toast.LENGTH_LONG).show();
}
答案 1 :(得分:2)
你不需要定义任何reg,因为android本身提供检查电子邮件的功能是有效的不是TextUtils.isEmpty(edtEmail.getText().toString().trim())
public static boolean isEmailValid(String email) {
String regExpn = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches())
return true;
else
return false;
}
希望对你有所帮助
答案 2 :(得分:0)
使用以下课程:
public class RequiredFieldValidator implements Validator {
/** The objects. */
List<EditText> objects;
/** The Constant EMAIL_ADDRESS_PATTERN. */
private final static 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}" + ")+");
/**
* Check email.
*
* @param email the email
* @return true, if successful
*/
public static boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
/**
* Instantiates a new required field validator.
*
* @param objects the objects
*/
public RequiredFieldValidator(List<EditText> objects) {
this.objects = objects;
}
/* (non-Javadoc)
* @see com.gangaz.mobi.supermarket.helper.Validator#setFailureMessage(java.lang.String)
*/
@Override
public void setFailureMessage(String message) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see com.gangaz.mobi.supermarket.helper.Validator#setType(int)
*/
@Override
public void setType(int type) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see com.gangaz.mobi.supermarket.helper.Validator#validate()
*/
@Override
public boolean validate() {
boolean valid = false;
for (EditText obj : objects) {
if (obj.getText().toString().trim().length() == 0) {
valid = false;
break;
}
}
return valid;
}
}
然后在你的活动中这样做:
if (email.length() != 0 &&
RequiredFieldValidator.checkEmail(email.trim()) != true) {
//show toast for invalid email
}
答案 3 :(得分:0)
初始化模式如下
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}" + ")+");
将以下功能复制到您的活动中
private boolean checkEmail(String email1) {
return EMAIL_ADDRESS_PATTERN.matcher(email1).matches();
}
使用电子邮件edittext String调用此函数。 该函数将结果写为TRUE或FALSE
答案 4 :(得分:0)
使用:
public boolean validateEmail(String strEmail) {
Matcher matcher;
String EMAIL_PATTERN = "[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}" + ")+";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(strEmail);
return matcher.matches();
如果返回true,则电子邮件无效。
答案 5 :(得分:0)
以下是我们应该处理的最简单,最好的方法,
EditText e1 = (EditText)findViewById(R.id.e1);
TextView t1 = (TextView)findViewById(R.id.t1);
String email = e1.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
// Below is the code that goes on your button click
if (email.matches(emailPattern))
{
// Do what you want to, Allow user to next screen
}
else
{
// Show error Message
}
答案 6 :(得分:0)
尝试:
String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
答案 7 :(得分:0)
试试这个......
public static boolean validateEmail(String email)
{
boolean isValid = false;
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())
{
isValid = true;
}
return isValid;
}
答案 8 :(得分:0)
试试这个
private boolean validEmail(String email)
{
// TODO Auto-generated method stub
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
String emailPatternnew = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+\\.+[a-z]+";
String domain = email.substring(email.indexOf('@'), email.length());
String last = domain.substring(domain.indexOf('.'),domain.length());
if (email.matches(emailPattern) && (last.length() ==3 || last.length() == 4)) // check @domain.nl or @domain.com or @domain.org
{
return true;
}
else if(email.matches(emailPatternnew) && last.length() == 6 && email.charAt(email.length()-3)== '.') //check for @domain.co.in or @domain.co.uk
{
return true;
}
else
{
return false;
}
}