Solved

String manipulation

Posted on 2004-04-03
12
410 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 250 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 86

Expert Comment

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

Assisted Solution

by:objects
objects earned 150 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 50 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 50 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

Independent Software Vendors: 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!

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

729 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