Email validation using java

How to make an email validation in Java?


This is my reference about email validation :

  1. cara membuat aplikasi validasi input.
  2. validasi email pada java.

// Use this library in Java for regex.  

import java.util.regex.Matcher;

import java.util.regex.Pattern;

// This is my input.  

String email=””;

// This code is metacharacter pattern for email.  

String regex = “^[\w-_\.+]*[\w-_\.]\@([\w-]+\.)+[\w]+[\w]$”;

//This code is for compile the pattern. It can be recognized by regex class 

Pattern pattern = Pattern.compile(regex);

// The input will match the email pattern. 

Matcher matcher;

matcher = pattern.matcher(email);

//This is validation code 

if(matcher.matches()) {

//If true, then return ” email valid “;

System.out.println(“email valid”);

} else{

//If false, then return “email is not valid”;

System.out.println(“email is not valid.Try again, please!”);



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