Solved

Java email address validation (Regular Expression)

Posted on 2004-10-26
390 Views
Last Modified: 2010-03-31
I'm working on a java based email address validator using regular expressions, and it seems to be working for the most part, but I have noticed two things that don't work properly.

Here is my regular expression

^([_a-zA-Z0-9-]+)(.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(.[a-zA-Z0-9-]+)*(.[a-zA-Z]{2,6})$

The two things that don't work with it are that it allows spaces, which I'm not sure why.  Although if there are two spaces in a row, then it does say theres a problem.

Secondly, the last part of it, where I specify a-zA-Z {2,6} is for the ".com" part of the email, where the length has to be between 2, and 6.  However when i run an email address it doesn't check the length properly.  the email address test@this.a, and it fails it properly because of the length 1.  But if I use the email test@this.asdfgasd it passes it, even though its of length 8.

I know how to get around the space problem by using a tokenizer of " ", and checking the tokenCount.  But is there a way to do it in the regularexpression?  and how do I fix the length part for the final piece?

Thanks
0
Question by:zaq
    5 Comments
     
    LVL 86

    Accepted Solution

    by:
    "." will match a space. It should be "\\."

    Check

        System.out.println(" ".matches("\\."));
        System.out.println(" ".matches("."));


    Why don't you use an off-the-shelf RE?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>Why don't you use an off-the-shelf RE?

    btw, i consider myself pretty strong with REs, but wouldn't dream of concocting my own email RE. Try

    final String EMAIL_RE = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$";
     
    0
     

    Author Comment

    by:zaq
    i looked online, and this expression was from a php site.  all I did was converted it to java, and aparently incorrectly at that.

    seems my understanding of regular expressions was incorrect.

    Changing it to: ^([_a-zA-Z0-9-]+)(\\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,6})$ made things work fine.  So the escape character of \\ in front of the . cleared up the space issue, and the count.  

    Thanks.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)

    >>i looked online, and this expression was from a php site

    Ah OK - it lost something in translation ;-)
    0
     

    Author Comment

    by:zaq
    yep.  my translation was just bad.

    Thanks again for the help.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This video teaches viewers about errors in exception handling.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now