How should I validate an e-mail address?

后端 未结 30 1644
臣服心动
臣服心动 2020-11-22 08:25

What\'s a good technique for validating an e-mail address (e.g. from a user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn\'t seem to be

相关标签:
30条回答
  • 2020-11-22 09:16

    Simplest Kotlin solution using extension functions:

    fun String.isEmailValid() =
                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}" +
                                ")+"
                ).matcher(this).matches()
    

    and then you can validate like this:

    "testemail6589@gmail.com".isEmailValid()
    

    If you are in kotlin-multiplatform without access to Pattern, this is the equivalent:

    fun String.isValidEmail() = Regex(emailRegexStr).matches(this)
    
    0 讨论(0)
  • 2020-11-22 09:16

    I have used follwing code.This works grate.I hope this will help you.

    if (validMail(yourEmailString)){
       //do your stuf
     }else{
     //email is not valid.
    }
    

    and use follwing method.This returns true if email is valid.

        private boolean validMail(String yourEmailString) {
        Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher emailMatcher = emailPattern.matcher(emailstring);
        return emailMatcher.matches();
    }
    
    0 讨论(0)
  • 2020-11-22 09:16

    You can do any type of validation in android very easily by the oval.jar file. OVal is a pragmatic and extensible general purpose validation framework for any kind of Java objects.

    follow this link: http://oval.sourceforge.net/userguide.html

    You can downlaod this from here: http://oval.sourceforge.net/userguide.html#download

    You can use validation by setting tags in variables

    public class Something{
    
        @NotEmpty  //not empty validation
        @Email     //email validation
        @SerializedName("emailAddress")
        private String emailAddress;
    }
    
       private void checkValidation() {
            Something forgotpass.setEmailAddress(LoginActivity.this.dialog_email.getText().toString());
            Validator validator = new Validator();
            //collect the constraint violations
            List<ConstraintViolation> violations = validator.validate(forgotpass);
            if(violations.size()>0){
                for (ConstraintViolation cv : violations){
                    if(cv.getMessage().contains("emailAddress")){
                        dialog_email.setError(ValidationMessage.formattedError(cv.getMessage(), forgotpass));
                    }
                }
            }
    }
    
    0 讨论(0)
  • 2020-11-22 09:17

    Try this code.. Its really works..

                if (!email
                        .matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"))
                {
                    Toast.makeText(getApplicationContext(), "Email is invalid",
                            Toast.LENGTH_LONG).show();
                    return;
                }
    
    0 讨论(0)
  • 2020-11-22 09:17

    Note that most of the regular expressions are not valid for international domain names (IDN) and new top level domains like .mobi or .info (if you check for country codes or .org, .com, .gov and so on).

    A valid check should separate the local part (before the at-sign) and the domain part. You should also consider the max length of the local part and domain (in sum 255 chars including the at-sign).

    The best approach is to transform the address in an IDN compatible format (if required), validate the local part (RFC), check the length of the address and the check the availability of the domain (DNS MX lookup) or simply send an email.

    0 讨论(0)
  • 2020-11-22 09:17

    email is your email-is.

    public boolean validateEmail(String email) {
    
        Pattern pattern;
        Matcher matcher;
        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(email);
        return matcher.matches();
    
        }
    
    0 讨论(0)
提交回复
热议问题