Validating Email, Mobile, Name in Java using Regex


This program validates Email, Mobile, Name in Java using Regex. We have to import Matcher, Pattern in order to use the classes. Regex is very simple and easy way to validate.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Class1 {
public Class1() {
}
static Pattern pattern=null;
static Matcher matcher=null;
public static void main(String[] args) {
System.out.println(Class1.emailValidation(“gowtham.raam@gmail.com”));
System.out.println(Class1.phonenNumberValidation(“944-3284989”));
System.out.println(Class1.nameValidation(“Gowtham Raam”));
// System.out.println(Class1.addressValidation(“213, Tecci Park, First Floor, Sholinganallur, chennai “));

Class1 class1 = new Class1();

}
public static String emailValidation(String email)
{
String patternvalidation=”^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@”+”[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$”;
pattern = Pattern.compile(patternvalidation);
matcher = pattern.matcher(“gowthamraam@gmail.com”);
if(matcher.matches())
{
return (“Pattern is Matched”+email);
}
else
{
return (“Pattern is Not Matched”+email);
}

}
public static String nameValidation(String name)
{
String patternvalidation=”[A-Z][a-z]+( [A-Z][a-z]+)”;// This pattern Validates like “Gowtham Raam” First Letter to be in InitCaps
pattern = Pattern.compile(patternvalidation);
matcher = pattern.matcher(name);
if(matcher.matches())
{
return (“Pattern is Matched”+name);
}
else
{
return (“Pattern is Not Matched”+name);
}

}
public static String phonenNumberValidation(String phonenumber)
{
String patternvalidation=”\\d{3}-\\d{7}”;
//If pattern should be without (-) symbol replace the String by “\\d{10}”
pattern = Pattern.compile(patternvalidation);

matcher = pattern.matcher(phonenumber);
if(matcher.matches())
{
return (“Pattern is Matched”+phonenumber);
}
else
{
return (“Pattern is Not Matched”+phonenumber);
}

}

// public static String addressValidation(String phonenumber)
// {
// String patternvalidation=”\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)”;
//
// pattern = Pattern.compile(patternvalidation);
//
// matcher = pattern.matcher(phonenumber);
// if(matcher.matches())
// {
// return (“Pattern is Matched”+phonenumber);
// }
// else
// {
// return (“Pattern is Not Matched”+phonenumber);
// }
//
// }
}

About pacesettergraam

A good and realistic person

Posted on August 8, 2013, in Java, Oracle. Bookmark the permalink. Leave a comment.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: