Search code examples
javaapache-commonsemail-validation

Apache commons email validator: allow plus character in e-mail


using org.apache.commons.validator.routines.EmailValidator (1.7) for e-mail validation in Java.

In GMAIL a user is allowed to use [email protected] and it will go to [email protected]. However, the EmailValidator does not allow the + character. I also tried replacing the + with %2b before validation but it also fails.

Any suggestions? Can I add an extra allowed character to my EmailValidator instance? Couldn't find this but guess I would need something like that.

Ps. I don't really want to go to custom regex for various reasons.


Solution

  • The Apache Commons EmailValidator does work with a plus sign.

    import org.apache.commons.validator.routines.EmailValidator;
    
    public class MapperMain {
    
    public static void main(String[] args) {
    
        String email = "[email protected]";
        
        EmailValidator emailValidator = EmailValidator.getInstance();
        
        
        System.out.println(emailValidator.isValid(email));
    
    }
    
    }
    

    Outputs:

    true