Java Email Validation29 Mar 2025 | 4 min read In designing forms, email plays an important role. The email can be of our username or login id. An email has its own structure, and before using it, we need to validate it. In Java, email validation is performed by using the regular expression. Email validation is required in any of the application that looks for email addresses as required information at the registration stage. There are five ways through which we can perform email validation using a regular expression.
![]() Simplest regex to validate an emailThe regular expression ^(.+)@(.+)$ is the simplest regular expression the checks the @ symbol only. It doesn't care about the characters before and after the '@' symbol. Let's take an example to understand how it validates the email address. EmailValidation1.java Output: ![]() Adding restriction on the User Name partThe regular expression "^[A-Za-z0-9+_.-]+@(.+)$" also check the user name part of the email address. In order to check the user name part of the email, we have added some restrictions by using a regular expression. The regex "^[A-Za-z0-9+_.-]+@(.+)$", ^[A-Za-z0-9+_.-] defines the following restriction.
Let's take an example to understand how it validates the email address. EmailValidation2.java Output: ![]() Email Validation Permitted by RFC 5322To validate the email permitted by RFC 5322, we use "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$" regular expression. It uses all the characters which are allowed by RFC for the email message format. In these characters, some characters present a risk when they pass directly from user input to an SQL statement. These characters are basically pipe character(|), single quote(''), and etc. Let's take an example to understand how it validates the email address. EmailValidation3.java Output: ![]() Restrict E-mail from Trailing, Consecutive, and LeadingThe regular expression "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" restrict us to add consecutive dots, trailing, and leading. An email can contain more than one dot in both the local part and the domain name, but consecutive dots are allowed. Our email can also not be started or ended with a dot. The regex validates the email based on these three conditions too. Let's take an example to understand how it validates the email address. EmailValidation4.java Output: ![]() Restrict email to enter a number of characters in the top-level domainThe regular expression "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$" check for at least one dot in the domain name and after the dot, it consist only the letters. The top-level domain should have only two to six letters which is also checked by this regex. Let's take an example to understand how it validates the email address. EmailValidation5.java Output: ![]() All the above discussed regular expressions are used for email validation. The last regular expression strictly validates the email with more conditions and rules. In Java, we use the last discussed regular expression mostly to validate the email. Next TopicDice-simulator-java-gui |
Generic is used for creating Java code for graphs. Java's HashMap class is used to implement the Graph class. As we know, HashMap has a key and a value; in the graph, nodes are represented as keys, and their adjacency is listed as values. What is Generic? Generics...
9 min read
A list is a type of data structure in programming that represents an ordered collection of elements. It allows storing and accessing elements sequentially and supports adding, removing, and retrieving elements. Lists are commonly used for organizing and manipulating data in various programming languages. Streams are a...
2 min read
? Java, being an object-oriented programming language, allows the use of reference variables to work with objects and their data. In Java, objects are created dynamically on the heap memory, and reference variables are used to hold the memory address of these objects. This concept of reference...
3 min read
Utilizing an instance variable when its value is unchanged is not a good idea. We may then apply a static modification to that variable at that point. Whenever we declare the variable static, a single variable is generated at the class level and shared by all...
4 min read
Branching statements are used to change the flow of execution from one section of a program to another. Branching statements are typically utilized within control statements. Java includes three types of branching statements: continue, break, and return. When a given condition is met, we can depart...
7 min read
In Java, the set is an interface that belongs to java.util package. The Set interface extends the Collection interface. An unordered collection or list in which duplicates are not allowed is referred to as a Collection interface. The set interface is used to create the...
15 min read
The Rotate Bits problem involves shifting the bits of an integer to the left or right, wrapping the overflowed bits to the opposite end. This operation is crucial in low-level programming, cryptography, and data manipulation tasks. Java provides bitwise operators to implement this efficiently for both...
7 min read
Data Access Object patterns, often known as DAO patterns, are used to divide high level business services from low level data accessing APIs or actions. The members of the Data Access Object Pattern are listed below. Data Access Object Interface: The Data Access Object Interface specifies the...
3 min read
In Java Interview Question, the most commonly asked question is how TreeMap works internally in Java or what is the internal implementation of TreeMap. In this section, we will learn how TreeMap works internally in Java. Before moving to the internal working, first, understand what is TreeMap. TreeMap...
4 min read
A queue is another kind of linear data structure that is used to store elements just like any other data structure but in a particular manner. In simple words, we can say that the queue is a type of data structure in the Java programming language...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India