aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Validation using Regular Expressions??" Watch "Validation using Regular Expressions??" New topic
Author

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
Greenhorn

Joined: Mar 10, 2004
Posts: 4
Im not exactly positive this is what you're looking for but i hope this code helps.
<CFFORM ACTION="">
Email: <CFINPUT TYPE="Text" NAME="email"
VALIDATE="regular_expression"
PATTERN=" ^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.
(([a-z]{2,3})|(aero|coop|info|museum|name)))?$"
MESSAGE="Email is improperly formatted">
<INPUT TYPE="Submit">
</CFFORM>
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.
http://www.sys-con.com/coldfusion/article.cfm?id=379&count=5104&tot=4&page=2
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1134

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:
MaoTseTung@china.com
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,
Avi.
Max Habibi
town drunk
( and author)
Sheriff

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 www.regexlib.com . They have great stuff, though you should validate it before using it, as they also have a lot of mistakes.
All best,
M
[ 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).


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation using Regular Expressions??