Two Laptop Bag
The moose likes Java in General and the fly likes Validation using Regular Expressions?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Validation using Regular Expressions??" Watch "Validation using Regular Expressions??" New topic

Validation using Regular Expressions??

Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
is it possible to validate whether a given email id to be a valid one ? or how should we go about in validating a email id?

Karthikeyan<br />SCJP 1.4, SCWCD.
Wael Aoudi

Joined: Mar 10, 2004
Posts: 4
Im not exactly positive this is what you're looking for but i hope this code helps.
Email: <CFINPUT TYPE="Text" NAME="email"
PATTERN=" ^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.
MESSAGE="Email is improperly formatted">
<INPUT TYPE="Submit">
Thank you,
Wael Aoudi
P.S. I got this from the following link which has a detailed explanation of CFForm. Therefore that code is not mine just to make it clear it is Charlie Areharrts.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

Hi Karthikeyan,
Assuming a valid eMail address contains alphanumeric characters, followed by the "@" character, followed by more alphanumeric characters -- separated with a period, for example:
Then the following is some java code to validate such an eMail address.
[Note, this is not a definitive answer -- it is only to help you understand how it works.]
My points of reference for arriving at the below code were:
'regex' page at Roedy Green's "Java Glossary"
javadoc of the "java.util.regex.Pattern" class

Good Luck,
Max Habibi
town drunk
( and author)

Joined: Jun 27, 2002
Posts: 4118
Hi Karthikeyan,
The answer to your question is yes: this is exactly the sort of thing that regex was designed for. As a matter of fact, it's the opening example in my book, right on page 1.
chances are, the pattern you're looking for lines along the following

However, if you need more robust checking( including IP email addresses, etc.), you'll find them in the book, along with explanations of how they're derived.
If you don't want to buy my book, try checking . They have great stuff, though you should validate it before using it, as they also have a lot of mistakes.
All best,
[ April 14, 2004: Message edited by: Max Habibi ]

Java Regular Expressions
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
thankyou friends,
i think i have to dive inside this topic and do some R&D. lets see how it turns out(i hope it will be able to replace a large prtion of code that i have been using for such validations and help the people who are going to do maintenance/enhancements in future)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Of course any checking that's really thorough will also involve some way to determine whether the domain at least is an existent one.
But that's beyond text processing RegEx does and goes into networking (attempt to do a DNS lookup for example).

subject: Validation using Regular Expressions??
It's not a secret anymore!