Android Email Validation on EditText

前端 未结 13 1047
陌清茗
陌清茗 2020-12-29 04:38

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



        
相关标签:
13条回答
  • 2020-12-29 05:09

    Try this code

    String emailPattern = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
            if (!tvEmail.matches(Constants.emailPattern)){
                tvEmail.setError("Invalid Email ");
            }
            else{
                //your code
            }

    0 讨论(0)
  • 2020-12-29 05:09

    Try below code:

    Just call below method like,

    if(emailValidator(mail.getText().toString())){
    Toast.makeText(getActivity(), "valid email address",
                        Toast.LENGTH_SHORT).show();
    
    }else{
    Toast.makeText(getActivity(), "invalid email address",
                        Toast.LENGTH_SHORT).show();
    
    }
    
        public static boolean emailValidator(final String mailAddress) {
    
                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(mailAddress);
                return matcher.matches();
    
            }
    
    0 讨论(0)
  • 2020-12-29 05:10

    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();
    }
    
    0 讨论(0)
  • 2020-12-29 05:18

    Android Email Validation Simplest way

       String validemail= "[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}" +
    
                            ")+";
    
                   String emal=email.getText().toString();
    
                    Matcher matcherObj = Pattern.compile(validemail).matcher(emal);
    
    
    
                            if (matcherObj.matches()) {
    
                                   Toast.makeText(getApplicationContext(), "enter 
    
    all details", Toast.LENGTH_SHORT).show();
    
                            }
    
                            else 
    {
                                Toast.makeText(getApplicationContext(),"please enter 
    
    valid email",Toast.LENGTH_SHORT).show();
    
                            }
    
    0 讨论(0)
  • 2020-12-29 05:18
    android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()
    
    0 讨论(0)
  • 2020-12-29 05:19

    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...

    0 讨论(0)
提交回复
热议问题