简体   繁体   中英

How to make validation for Email in android?

I am developing an app in which I have email edit text field in which I make some validation it works but not properly when I enter email like jj@gm.cim it except that but this is wrong right validation is jj@gmail.com .

How can I do like that?

Here is code:-

public boolean validation(String mobile,String pass,String email){
    if (mobile!=null && mobile.length() >= 7 && mobile.length() <= 15) {
        if (pass.length() >= 4 && pass.length() <= 8) {
            if (isValidEmail(email)) {
                return true;
            } else {
                m_EmailEditText.setError("Invalid EmailID");
                return false;
            }
        } else {
            m_PasswordEditText.setError("password must be between 4 to 8 characters long");
            return false;
        }
    } else {
        m_MobileEditText.setError("mobile number must be between 7 to 51 characters long");
        return false;
    }
}
// This is validation for email……….and link to above code….
private boolean isValidEmail(String email) {
    String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    Pattern pattern = Pattern.compile(emailPattern);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

If you want to use regex then try this way to get valid email

public static 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;
}

OR if you want to use default validation try this way

boolean isEmailValid(CharSequence email) {
   return android.Util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

You could also use,

public class EmailValidator {

private Pattern pattern;
private Matcher matcher;

private static final String EMAIL_PATTERN = 
    "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

public EmailValidator() {
    pattern = Pattern.compile(EMAIL_PATTERN);
}

/**
 * Validate hex with regular expression
 * 
 * @param hex
 *            hex for validation
 * @return true valid hex, false invalid hex
 */
public boolean validate(final String hex) {

    matcher = pattern.matcher(hex);
    return matcher.matches();

}
 }

Function for validating the EmailID

private 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;

}

Refer android Patterns

It`s android inbuild regular expression method.

https://developer.android.com/reference/android/util/Patterns.html

public final static boolean isValidEmail(CharSequence target) {
 return !TextUtils.isEmpty(target) &&   android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

Answer from this SO link - https://stackoverflow.com/a/7882950/3879847

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM