-1

To validate email I am using following method i.e. official java email package

public static boolean isValidEmailAddress(String email) {
boolean result = true;
try {
  InternetAddress emailAddr = new InternetAddress(email);
  emailAddr.validate();
} catch (AddressException ex) {
  result = false;
}
return result;
}

But above method also considers true for even "user@localhost.com", "user@10.9.8.7? I will be grateful if someone please help me out in removing above all these while validating email id of a user? I searched in google but could not find any solution. Many thanks in advance

Tom
  • 761
  • 7
  • 22
  • 41
  • Spell out your complete rules. What is actually wrong with `bla@bla.com`? – PM 77-1 Aug 06 '19 at 16:40
  • Ok! I have removed that , it's fine bla@bla.com – Tom Aug 06 '19 at 16:41
  • The requirement still stands - in order to implement something meaningful you need to know what exactly. You need rules - a couple of samples won't do. – PM 77-1 Aug 06 '19 at 16:45
  • ya I understand your point...at least it should remove user@10.9.8 like this and localhost.... – Tom Aug 06 '19 at 16:48
  • @PM77-1 hi i have a doubt actually while initializing boolean variable i should declare false right not true ? like boolean result = false; – Tom Aug 06 '19 at 17:12
  • Those are valid emails per the RFC. If you disagree with that then you can cobble together your own rules for a stricter email validator. If you can coherently present which rules you wish to enforce then regex can help but as of now your question is simply a non-issue. – MonkeyZeus Aug 06 '19 at 17:39
  • See https://stackoverflow.com/q/201323/2191572 for more information. – MonkeyZeus Aug 06 '19 at 17:40

1 Answers1

0

The official Java email package considers things like user@localhost.com and user@10.9.8.7 as valid email addresses, since they are according to the RFC. Like the people who commented said, it would be best to figure out what rules you're trying to implement for your validation and go from there.

dacmacho
  • 45
  • 5