-5

Validate email format only if EditText is not blank in Android. And if the field is blank validation should not be check. I did not find any solution in this scenario which is useful for me.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Adi
  • 400
  • 8
  • 25

7 Answers7

1

Try this code

final EditText emailEditTxt= (EditText)findViewById(R.id.text); 

String emailStr = emailEditTxt.getText().toString().trim();

if(emailStr!=null)

if(emailStr.length()>=1){

String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";


if (emailStr .matches(emailPattern))
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else 
{
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
}
}
Android Surya
  • 544
  • 3
  • 17
1

Try this..

boolean flag;
String pass = editText.getText().toString().trim();
if(!pass.equals("")) {
    flag = isEmailValid(pass);
}

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

and use the flag value for further use

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
0

Use this method for check email pattern is valid or not

public static boolean isEmailValid(CharSequence email) {
        return Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }
Nitesh Pareek
  • 362
  • 2
  • 10
0

validateEmail

private boolean validateEmail() {
 if (!edemail.getText().toString().trim().isEmpty()&&!ValidationMethod.emailValidation(edemail.getText().toString()))
    {

        input_layout_email.setError("Invalid Email"); // Your textInput Layout or other set your error on edittext email
        requestFocus(edemail);
        return false;
    }

    else {
        input_layout_email.setErrorEnabled(false);
    }

    return true;
}

   // if you need requestfocus other wise remove
 private void requestFocus(View view) {
    if (view.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}

ValidationMethod.java // put in your project

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidationMethod {

    static Matcher m;
    static String emailExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,20}$";
    static Pattern emailPattern = Pattern.compile(emailExpression, Pattern.CASE_INSENSITIVE);
    static String passwordExpression ="((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\\W).{8,20})";
    static Pattern passwordPattern=Pattern.compile(passwordExpression);

    public static boolean emailValidation(String s)
    {
        if( s == null)
        {
            return false; 
        }
        else
        {
            m = emailPattern.matcher(s);
            return m.matches();
        }
    }

    public static boolean passwordValidation(String s)
    {
        if( s == null)
        {
            return false; 
        }
        else
        {
            m = passwordPattern.matcher(s);
            return m.matches();
        }
    }

    public static boolean emailValidation2(String s)
    {
        m = emailPattern.matcher(s);
        return m.matches();
    }
}
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

try This

<form>
    <lable for="userEmail">Email : </label>
    <input type="email" id="userEmail" placeholder="johndoe@example.com" required="required">
<form>
0

Lets say you have declared EditText like this.

EditText emailField;

then in your method

emailField = (EditText)view.getViewById(R.id.NAME_OF_FIELD_IN_XML);
if(!TextUtils.isBlank(emailField.getText().toString())){
     //validate your email address
}

Use below link for email validation code.

http://stackoverflow.com/questions/12947620/email-address-validation-in-android-on-edittext
Dishant Anand
  • 362
  • 1
  • 14
0

You can try this for validation.

    public boolean Email_validation(String CorpId)
{
    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 = CorpId;

    Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);

    if(matcher.matches())
        return true;
    else
        return false;
}
amit pandya
  • 1,384
  • 13
  • 22