Validating email using a RegEx

02 Mar

Hi folks,

In day today development world, we usually required to do mandatory validation for some fields. One of the common field is email address.

The following code demonstrates how to validate email using regular expression:

import java.util.regex.*;
class regexSample
public static void main(String args[])
//Input the string for validation
String email = “”;
//Set the email pattern string
Pattern p = Pattern.compile(“.+@.+\\.[a-z]+”);
//Match the given string with the pattern
Matcher m = p.matcher(email);
//check whether match is found
boolean matchFound = m.matches();
if (matchFound)
System.out.println(“Valid Email Id.”);
System.out.println(“Invalid Email Id.”);

Note: In my all post code snippets, used System.out.println() to print the output. Make sure that once you are fine with the coding, remove all SOPs from the file and then do the checkins. As per the SONAR code review standards this is highly blocker issue.

Happy Coding!.

keep tuning to know more updates.

Have a nice day 🙂

Leave a comment

Posted by on March 2, 2013 in Uncategorized


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: