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: An email validation in Java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default An email validation in Java

    HI Java professionals,
    I want to validate an email input. The email input can only have one @ and at least one '.' after the @.
    I would like to ask if my regex pattern is correct, thanks.
    YOUR CODE HERE
    import java.util.InputMismatchException;
    import java.util.Scanner;
    public class email
    {
        public static void main(String []args)
        {
            Scanner reader =new Scanner(System.in);
            String input;
            while(true){
                try{
                    System.out.print("Enter an email address:");
                    input=reader.next();
                    break;
                }catch(InputMismatchException e)
                {
                    System.out.println("It is not an email address.");
                    reader.nextLine();
                }
            }
            boolean test = isValidEmailAddress(input);
            System.out.println(test);
        }
     
        public static boolean isValidEmailAddress(String email) {
            java.util.regex.Pattern p = java.util.regex.Pattern.compile("^[(a-zA-Z-0-9-\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$");
            java.util.regex.Matcher m = p.matcher(email);
            return m.matches();
        }
    }
    Last edited by Loh Jane; April 5th, 2014 at 07:38 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: An email validation in Java

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    One way to see if the program does what you want is to thoroughly test it.

    What happens when you test it? Did it work as desired or did you find cases where it failed?
    Post the cases where it fails.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: An email validation in Java

    Yes it works finely but I need to submit it.So I want to confirm if the pattern is correct.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: An email validation in Java

    With regex it is correct only until you get 1 case that causes it to fail. In your case, if you have a finite set of e-mail addresses that the regex must match and/or filter, then you just need to test with them. This is especially true with e-mail addresses as although there is a standard that describes the valid syntax for e-mail addresses, it is too complicated, and it still doesn't filter out all invalid e-mail addresses. See How to Find or Validate an Email Address for a full discussion on this. You may find the examples at Email address - Wikipedia, the free encyclopedia to be useful if you're not given a set of e-mail addresses to test against. (An Internet search using the search term "example email address" will give you more.

    Have you used/do you want to learn unit testing? From experience in professional software development, unit testing (using a framework such as JUnit or TestNG) is indispensable when writing regexes. It not only gives you a way to quickly test all known success and failure cases, it also provides automatic regression testing whenever you need to modify the regex to ensure the modification doesn't break what used to work before.

Similar Threads

  1. Bulk email sendingbulk email
    By Bra_Zee in forum Java Theory & Questions
    Replies: 0
    Last Post: March 6th, 2013, 09:06 AM
  2. Java Code Send email from email id configured in outlook
    By Hrithik in forum Java Theory & Questions
    Replies: 0
    Last Post: September 21st, 2012, 02:41 AM
  3. how to do validation using java swing?
    By Vignesh Karthick in forum AWT / Java Swing
    Replies: 1
    Last Post: January 24th, 2011, 06:32 AM
  4. how to do validation using java swing?
    By Vignesh Karthick in forum Member Introductions
    Replies: 1
    Last Post: January 24th, 2011, 06:32 AM