?
Solved

String manipulation

Posted on 2004-04-03
12
Medium Priority
?
413 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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