-2

hello guys is android support regex operation like C# or its exist with different name if it is can you tell me what is it and what i want is to do regex operation to check if this is valid e-mail on EditText ?

Dexter90
  • 69
  • 2
  • 14
  • actually i am making some research for it but could not find anything i am new android developer if you can tell me the keyword that i need to search for it – Dexter90 Sep 03 '14 at 10:43
  • there are a lot of answer does all the answers works? – Dexter90 Sep 03 '14 at 10:45
  • well this [which explains why not to use regex.](http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address) explains that if email address is _user@gmail.com.nospam_ regex operation fails, the same regex expression which meets the [RFC](http://tools.ietf.org/html/rfc2822#section-3.4.1) – Sagar Pilkhwal Sep 03 '14 at 10:51
  • you can refer this [textbox email address validation](http://stackoverflow.com/questions/12947620/email-address-validation-in-android-on-edittext) or this [which explains why not to use regex.](http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address) – Sagar Pilkhwal Sep 03 '14 at 10:53
  • possible duplicate of [Android: How can I validate EditText input?](http://stackoverflow.com/questions/2763022/android-how-can-i-validate-edittext-input) – Sagar Pilkhwal Sep 03 '14 at 10:54

2 Answers2

1

Android provide inbuilt email pattern matcher so use that this code double check for blank email and email validation :) short and sweet

public boolean isValidEmail(CharSequence strEmail) {
    return !TextUtils.isEmpty(strEmail) && android.util.Patterns.EMAIL_ADDRESS.matcher(strEmail).matches();}
MilapTank
  • 9,988
  • 7
  • 38
  • 53
0
    public boolean emailValidator(String email) 
    {
        Pattern pattern;
        Matcher matcher;
        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.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(email);
        return matcher.matches();
    }

Try this. Pass the string which has to be validated to emailValidator function

        if(emailValidator(editText.toString()))
        {
            Log.d("Success", "yes");
        }
Jossy Paul
  • 1,267
  • 14
  • 26