Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

email validation using java code ."taking space."

 
Mannu Pal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am using this code for email validation. the only problem with this is its taking space like as i have shown in exp code.
can any one help me to fix this bug.

String emailString = "aa hello@abv.com";
boolean isValidEMail = false;
String emailRegEx ="^([_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*.(([a-z]{2,3})|(aero|coop|info|museum)))?$";
Pattern pattern = Pattern.compile(emailRegEx);
Matcher matcher = pattern.matcher(emailString);
isValidEMail = matcher.find();
System.out.println("email result : "+isValidEMail);

email result : true

Regards ,
Mannu Pal.
 
Mannu Pal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i got the solution.
just replace this line of code and enjoy !!!

String emailRegEx ="^([_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*\\.(([A-Za-z]{2,3})|(aero|coop|info|museum)))?$";


Regards ,

Mannu Pal
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valid email address can have spaces. See the specification. The JavaMail API also supplies a class that will do the validation for you.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called javax.mail.internet.InternetAddress, and it is quite powerful. It even has methods to convert a list of email addresses (separated by commas) to InternetAddress[].
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic