Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String manipulation

Posted on 2004-04-03
12
Medium Priority
?
415 Views
Last Modified: 2013-11-24
What is a good way to test if a user entered value is in a valid email format? i.e. myName@myDomain.com
0
Comment
Question by:tricks801
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10749783
Try

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

boolean validEmail = input.matches(MAIL_RE);
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 10749799
Hang on - have to Java-fy that:

final String MAIL_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:tricks801
ID: 10749810
i received an illegal escape character at some . and -
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10749821
Did you miss my correction?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 600 total points
ID: 10749823
this'll do a bit better, add any domains as appropriate.

  public static boolean isValidEmailAddress(String email)
  {
    String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[_A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*\\.(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)";
    return email.matches(regex);
  }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10749842
>>this'll do a bit better

Why so - in what way?
0
 
LVL 16

Assisted Solution

by:gnoon
gnoon earned 200 total points
ID: 10751457
If you already have javamail package

public boolean isValidEmailAddresses(String addresslist) {
    javax.mail.internet.InternetAddress addresses[];
    try {
        addresses = javax.mail.internet.InternetAddress.parse(addresslist, false);
    } catch(javax.mail.internet.AddressException e) { return false; }
    return (addresses != null && addresses.length > 0);
}

you can test the value for valid email address list string e.g. "user@dom.org" or "user1@dom1.org, user2@dom2.org".
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 200 total points
ID: 10751472
you need to put international domains too:

"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[_A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*\\.(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|be|ch|co|de|es|fr|it|jp|kr|ru|se|sk|uk ..............

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10751595
>>If you already have javamail package

AFAIK the package only makes relatively crude checks on address validity
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10752976
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10753522
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10755802
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

597 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