电子邮件有效性检查

时间:2013-11-28 06:25:37

标签: android

我使用此代码检查我的电子邮件的有效性。然而,这是有效的。它总是一遍又一遍地显示无效的电子邮件。

private boolean isValidEmail(String email){
    boolean isValidEmail = false;

    String regExpn = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

    CharSequence inputStr = email;

    Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (email.matches(regExpn))
    {
        isValidEmail = true;
    }
    return isValidEmail;
}

这是方法调用

else if(!isValidEmail(edit_txt_EmailAddress.toString().trim()))
{
 showAlertDialog(Login.this, "Email", "Enter Valid Email", false);
 edit_txt_EmailAddress.requestFocus();
}

6 个答案:

答案 0 :(得分:4)

Android内置模式匹配器。您可以通过以下代码尝试此操作。

public final static boolean isValidEmail(CharSequence target) {
        if (target == null) {
            return false;
        } else {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
        }
    }

我已经尝试过了,它正在发挥作用。

希望这会有所帮助。快乐的编码:)

答案 1 :(得分:1)

使用以下方法:

private boolean validEmail(String email) {
        Pattern pattern = Patterns.EMAIL_ADDRESS;
        return pattern.matcher(email).matches();
    }

在代码中写下以下代码:

if (!validEmail(registerEmailET.getText().toString())) {
                    //show error
Toast.makeText(this,"Invalid Email",Toast.LENGTH_LONG).show;
                    return;
                }

答案 2 :(得分:0)

检查一下:

public static boolean isEmailValiddate(String email) {
Pattern pattern = Pattern
 .compile("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b");
Matcher matcher = pattern.matcher(email);
if (!matcher.find()) {
return false;
}
return true;
}

答案 3 :(得分:0)

public static final boolean isEmailValid(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;
}

答案 4 :(得分:0)

更改方法调用:

  

isValidEmail(edit_txt_EmailAddress.getText()。的toString()。修剪())

答案 5 :(得分:0)

用于构建匹配器进行验证。

if(android.util.Patterns.EMAIL_ADDRESS.matcher("xyz@gmail.com").matches()){

}
else{
        //invalid
    }

注意:它的Api等级为8级以上。

Alterante#使用正则表达式,尝试下面的代码:

// BOOLEAN METHOD TO CHECK THE PATTERN OF THE EMIL-ID INPUT BY THE USER
    public static boolean isEmail(String email) {
        boolean matchFound1;
        boolean returnResult = true;
        email = email.trim();
        if (email.equalsIgnoreCase(""))
            returnResult = false;
        else if (!Character.isLetter(email.charAt(0)))
            returnResult = false;
        else {
            Pattern p1 = Pattern.compile("^\\.|^\\@ |^_");
            Matcher m1 = p1.matcher(email.toString());
            matchFound1 = m1.matches();


            Pattern p = Pattern
                    .compile("^[a-zA-z0-9._-]+[@]{1}+[a-zA-Z0-9]+[.]{1}+[a-zA-Z]{2,4}$");
            // Match the given string with the pattern
            Matcher m = p.matcher(email.toString());

            // check whether match is found
            boolean matchFound = m.matches();

            StringTokenizer st = new StringTokenizer(email, ".");
            String lastToken = null;
            while (st.hasMoreTokens()) {
                lastToken = st.nextToken();
            }
            if (matchFound && lastToken.length() >= 2
                    && email.length() - 1 != lastToken.length()
                    && matchFound1 == false) {

                returnResult = true;
            } else
                returnResult = false;
        }
        return returnResult;
    }