How should I validate an e-mail address?

后端 未结 30 2046
臣服心动
臣服心动 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 08:51

    Following was used by me. However it contains extra characters than normal emails but this was a requirement for me.

    public boolean isValidEmail(String inputString) {
        String  s ="^((?!.*?\.\.)[A-Za-z0-9\.\!\#\$\%\&\'*\+\-\/\=\?\^_`\{\|\}\~]+@[A-Za-z0-9]+[A-Za-z0-9\-\.]+\.[A-Za-z0-9\-\.]+[A-Za-z0-9]+)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputString);
        return matcher.matches();
    }
    

    Answer of this question:- Requirement to validate an e-mail address with given points

    Explanation-

    1. (?!.*?..) "Negative Lookhead" to negate 2 consecutive dots.
    2. [A-Za-z0-9.!#\$\%\&\'*+-/\=\?\^_`{\|}\~]+ Atleast one characters defined. ("\" is used for escaping).
    3. @ There might be one "@".
    4. [A-Za-z0-9]+ then atleast one character defined.
    5. [A-Za-z0-9-.]* Zero or any repetition of character defined.
    6. [A-Za-z0-9]+ Atleast one char after dot.

提交回复
热议问题