0

I want to validate an email introduced inside an EditText and this the code that I already have :

public static boolean isValidEmail(String str) {
    boolean isValid = false;
    String expression = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}

The problem is that when I am using _ and - these characters in starting of email id, no error is showing, but when i use any other character, an error is shown.

Now i want that when ever i used any special character in starting of email id, an error must be shown. no special character should be allowed in starting of email id.

what should i do for this..?

Amardeepvijay
  • 1,626
  • 3
  • 17
  • 47
  • `amardeep@your-company.com` is also the valid email id. – Rethinavel Mar 31 '14 at 07:46
  • [http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/](http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/) – S.Thiongane Mar 31 '14 at 07:47
  • @RethinavelPillai when i used -amardeep@gmail.com or _amardeep@gmail.com, no error is shown. – Amardeepvijay Mar 31 '14 at 07:50

3 Answers3

1

try this simple code:

    public static boolean validateEmailAddress(String emailAddress) {    
        regexPattern = Pattern .compile("^[(a-zA-Z-0-9-\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$");
        regMatcher = regexPattern.matcher(emailAddress);
        if (regMatcher.matches())
            {
            return true;
        } else {
            return false;
        }
      }
Amardeepvijay
  • 1,626
  • 3
  • 17
  • 47
Siruk Viktor
  • 504
  • 1
  • 8
  • 30
  • when i used %amar@gmail.com email Id , its shown error but when i used -amardeep@gmail.com and _amardeep@gmail.com, no error is shown. – Amardeepvijay Mar 31 '14 at 08:00
  • @Amardeep special symbol in the address - it's a bad idea that can lead to an error in the system e-mail. But if you really need, then you can define a list of available characters in any of the group - for example as follows: "^[(a-zA-Z-0-9-\\u0025\\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$" – Siruk Viktor Mar 31 '14 at 08:16
  • i dont want special character in email id. when i used any special character its shown error but when i used _ and - its accept. dont shown error msg – Amardeepvijay Mar 31 '14 at 08:22
  • @Amardeep ,because the characters _ and. are permitted for use in e-mail addresses. If you do so you must leave it in the group only characters that you allow: (a-zA-Z-0-9) etc. – Siruk Viktor Mar 31 '14 at 08:30
1

The pattern used to validate an email address in the android sdk is

    public static final Pattern EMAIL_ADDRESS
        = 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}" +
            ")+"
        );

you can access it this way:

 android.util.Patterns.EMAIL_ADDRESS
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Simply you can create a method and check it before accepting the input . There's no need for manual check of the email input characters android has all these data types defined you just need to call it Example

public static boolean isEmailValid(CharSequence email)
    { 
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() ; 

    } 

Then on the oncreate or if within the button etc just call it like

 EditText et = (EditText)findViewbyid(R.id.email) ;
 String em = et.getText().toString() ;
 if(!Validating.isEmailValid(em))
      {
         et.setHint("use this format username@abc.com") ;
          return;
      } 
Nasz Njoka Sr.
  • 1,138
  • 16
  • 27