-2

I have problem with emails validation in Apache Commons Validatior:

john@newman.com => true
john@newman.COM => false
john@newman.Com => false

Why does character size matter?

EDIT: Thank you all for help. Problem wasn't in Apache Commons but in my code.

Dawid Wekwejt
  • 533
  • 1
  • 4
  • 19
  • @Henry I know, but I don't want to do workarounds for this moment and I want to understand why I have those result. – Dawid Wekwejt Feb 05 '19 at 11:08
  • 1
    It is open source, check the source code. It is telling you why it behaves this way. – Henry Feb 05 '19 at 11:09
  • Apache commons email validator already converts the given email to lower case before matching against valid patterns, hence case won't matter. – Vijay Muvva Feb 05 '19 at 11:15

2 Answers2

1

Try below code. I tried with your input with commons-validator-1.6.jar and all worked for me.

EmailValidator valid=EmailValidator.getInstance();
String mail="john@newman.com";
String mail1="john@newman.COM";
String mail2="john@newman.Com";
if(valid.isValid(mail)) {
    System.out.println("Valid Mail : "+mail);
} else {
    System.out.println("InValid Mail : "+mail);
}

output :

john@newman.com => valid
john@newman.COM => valid
john@newman.Com => valid
J-Alex
  • 6,881
  • 10
  • 46
  • 64
Srikanth
  • 11
  • 2
1

The case does not matter with Apache commons email validator, here is the sample code

import org.apache.commons.validator.routines.EmailValidator;

public class Main {

    public static void main(String[] args) {

        EmailValidator validator = EmailValidator.getInstance();

        if (validator.isValid("john@newman.COM")) {
            System.out.println("Valid");
        } else {
            System.out.println("Invalid");
        }
    }
}

I have tested this code with commons-validator-1.6.jar and the emails john@newman.COM, john@newman.Com are valid as per the code.

 Output - Valid

Apache commons validator internally converts the email to lower case before matching the patterns for a valid email, hence the case will not matter.

Vijay Muvva
  • 1,063
  • 1
  • 17
  • 31