How to Validate Email Address in Java


Here this tutorial would help us learn how to validate email address with regular expression in Java.

Source Code

package com.beginner.examples;

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

public class ValidateEmail {

	public static void main(String[] args) {
		String email = "[email protected]";
		isNameAdressFormat(email);
	}
	
	private static boolean isNameAdressFormat(String email){
        boolean isExist = false;
     
        Pattern p = Pattern.compile("w+@(w+.)+[a-z]{2,3}");
        Matcher m = p.matcher(email);
        boolean b = m.matches();
        if(b) {
            System.out.println("Valid mail address");
            isExist=true;
        } else {
            System.out.println("Invalid mail address");
        }
        return isExist;
    }

}

Output:

Valid mail address

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments