-2

I have an edit text and have implemented some validation for this but when I type "RRRR@RRRRFF.cOM" it will accept that value.How can I do that

code:-

/*validation for email*/
private boolean isValidEmail(String email) {// validation for email Id
    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();
}
Siddharth
  • 181
  • 2
  • 11

3 Answers3

0

try this one:

Pattern.compile(Patterns.EMAIL_ADDRESS.toString()).matcher(textView.getText().toString()).matches()
Vietnt134
  • 512
  • 7
  • 19
0

Use this code.

 if (!android.util.Patterns.EMAIL_ADDRESS.matcher(emailAddress.getText().toString()).matches()) {
                        Toast.makeText(EditProfileActivity.this, getString(R.string.plzenter_valid_email), Toast.LENGTH_SHORT).show();
                        return;
                    }
pradip_android
  • 283
  • 1
  • 3
  • 16
0

Try this code. its works like a charm

    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;