Android Email Validation on EditText

2019-02-04 05:20发布

hello everyone i have one edittext and i would to to write email validation in my Editttext this is a xml code

<EditText
        android:id="@+id/mail"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignLeft="@+id/phone"
        android:layout_below="@+id/phone"
        android:layout_marginRight="33dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/edit_background"
        android:ems="10"
        android:hint="E-Mail"
        android:inputType="textEmailAddress"
        android:paddingLeft="20dp"
        android:textColor="#7e7e7e"
        android:textColorHint="#7e7e7e" >
    </EditText>

and this is a java code

emailInput = mail.getText().toString().trim();

    emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";


    if (emailInput.matches(emailPattern)) {
            Toast.makeText(getActivity(), "valid email address",
                    Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(getActivity(), "Invalid email address",
                    Toast.LENGTH_SHORT).show();
            mail.setBackgroundResource(R.drawable.edit_red_line);
        }

i can't validation.the toast message is always "Invalid email address" what am i doing wrong? if anyone knows solution please help me

11条回答
干净又极端
2楼-- · 2019-02-04 06:09

Use this function for validating email id:

 private boolean validateEmaillId(String emailId){

    return Pattern.compile("^(([\\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})$").matcher(emailId).matches();
}
查看更多
Luminary・发光体
3楼-- · 2019-02-04 06:12

Try following code:

public final static boolean isValidEmail(CharSequence target) {
    return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

This works fine.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-04 06:17

I am posting very simple and easy answer of email validation without using any string pattern.

1.Set on click listener on button....

 button_resetPassword.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            CharSequence temp_emilID=username.getText().toString();//here username is the your edittext object...
            if(!isValidEmail(temp_emilID))
            {
                username.requestFocus();
                username.setError("Enter Correct Mail_ID ..!!");
                     or
                Toast.makeText(getApplicationContext(), "Enter Correct Mail_ID", Toast.LENGTH_SHORT).show();

            }
            else
           {
              correctMail.. 
             //Your action...

           }

         });

2.call the isValidEmail() i.e..

   public final static boolean isValidEmail(CharSequence target)
   {
    if (TextUtils.isEmpty(target))
    {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
 }

I hope it will helpful for you...

查看更多
我想做一个坏孩纸
5楼-- · 2019-02-04 06:19

Why not use:

public final static boolean isValidEmail(CharSequence target) {
  return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

As suggested here.

查看更多
女痞
6楼-- · 2019-02-04 06:20

I had a query in email pattern for more than one Email ID validation and for only one Email ID.I solved it by using :

public static final String patter_emails="^(\\s*,?\\s*[0-9a-za-z]([-.\\w]*[0-9a-za-z])*@([0-9a-za-z][-\\w]*[0-9a-za-z]\\.)+[a-za-z]{2,9})+\\s*$";
public static final String patter_email="^(\\s*[0-9a-za-z]([-.\\w]*[0-9a-za-z])*@([0-9a-za-z][-\\w]*[0-9a-za-z]\\.)+[a-za-z]{2,9})+\\s*$";

These above is used for pattern in Java and Android both.

check this by using:

pattern test: rubular.com

查看更多
登录 后发表回答