Android email validation || Valid email id

 

Android email validation :

In android apps we generally use email-id’s to make user login, or to perform any other operations required for users. So we need to validate the email address before we accept it thought we cannot confirm the validity of the email address but we can confirm the pattern of the email address.

We can check with the android email validation pattern available and can let the user know instantly to rectify the email address according to the prescribed pattern.

We need to validate the fields so as to avoid unnecessary network calls and also it’s a best practice.Not only email field we need to validate all the fields.

 

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,})$";

 

According to the above pattern we may get email address validated according to this format  –> abc@email.com

 

Initialize pattern and matcher

Using a Boolean return method you can validate email address.

 

private static Pattern pattern;
private static 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 static boolean isValidEmail(EditText editText){
      pattern = Pattern.compile(EMAIL_PATTERN);
      matcher = pattern.matcher(editText.getText().toString());

    if(!matcher.matches())
    editText.setError("Enter valid email");

    return matcher.matches();

}

 

So in this way we undergo android email validation if you have any queries let me know in the comment section below.

If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
Android ButterKnife Tutorial || Data Binding

  Android ButterKnife : An easier and simple way to bind UI components in the android screens to the code...

Close