6

如何进行电子邮件验证?我使用以下代码来检查电子邮件的验证。

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.

我需要验证这一点。我怎样才能验证这一点?请帮我。

4

9 回答 9

31

使用此代码块 将电子邮件传递给此函数,它将返回一个布尔值 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();

}
于 2013-09-20T06:08:56.467 回答
1

你不需要定义任何 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;
    }

希望对你有帮助

于 2013-09-20T06:14:40.970 回答
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;
}
于 2013-09-20T06:03:04.057 回答
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;
    }
}
于 2013-09-20T06:13:22.093 回答
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

                }
于 2013-09-20T05:55:26.820 回答
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();
    }

并使用电子邮件编辑文本字符串调用此函数。此函数将结果写入 TRUE 或 FALSE

于 2013-09-20T05:57:20.403 回答
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,则电子邮件有效,否则无效。

于 2013-09-20T05:57:30.860 回答
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
}
于 2013-09-20T05:58:04.967 回答
0

尝试:

String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
于 2013-09-20T06:00:17.260 回答