Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java program to validate an email address using Regular Expressions

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Java program to validate an email address using Regular Expressions

    With this code you can check the validity of an email address using Regular Expressions.

    import java.util.regex.*;
     
    public class EmailValidator{
     
    public static void main(String[] args){
     
     String email = "checkme@youremail.com"; 
     String regEx = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
     
     Pattern p = Pattern.compile(regEx);
     Matcher m = p.matcher(email);
     
     if(m.find()) 
     {
      System.out.println(email + " is a valid email address."); }
     else
     {
      System.out.println(email + " is a invalid email address");
     }
     }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers


  2. #2
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Validate an email address using Regular Expressions

    Thanks for the post. I have not tested the code yet, but using InternetAddress is much more simple.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 03:06 PM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to Validate an email address using Regular Expressions

    Quote Originally Posted by cafeteria84 View Post
    Thanks for the post. I have not tested the code yet, but using InternetAddress is much more simple.
    Feel free to post other solutions.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Validate an email address using Regular Expressions

    The following code strictly validates an email address agants the RFC822 syntax :

    try {
         InternetAddress addr = new InternetAddress(email, true);
         // valid email
    } catch (AddressException ex) {
         // invalid email
    }

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 03:07 PM.

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 11:49 AM
  2. How to get your computer name and IP address?
    By JavaPF in forum Java Networking Tutorials
    Replies: 7
    Last Post: December 8th, 2011, 11:36 AM
  3. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 05:53 PM
  4. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 6th, 2008, 02:03 AM