Solved

String manipulation

Posted on 2004-04-03
12
405 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 58
Checkbox and ListView in Android Layout 4 56
maven project error 5 48
why cannot we forward request once the response is committed 2 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

867 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

23 Experts available now in Live!

Get 1:1 Help Now