Java Email ValidationIn 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 TopicJava Testing Tools |
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