Solved

String manipulation

Posted on 2004-04-03
12
404 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
nestparen challenge 4 56
PDFBox - convert image to PDF, PDF resolution 3 66
groovy example issue 10 66
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now