Java Example Programs – How to write a Email Validation program in Java

email-validation-in-java

Hey Folks, Welcome to practice house, as we all know this is the house for programming practices. So Today we are going to learn about Email Validation.

Email Validation is really important when we want to do some development task. Even when we develop any web application for registration we need to do the email validation. Normally it’s for that we should not trust user they can dump you any stupid data that doesn’t make any sense. So from our side, we should do some validation always.

So Let’s See the Code How it works. If you don’t understand the code then down there it has a small explanation please read through. If you don’t understand and you want to learn more please comment’s below.

Here is the code:

Code Output Sample:

Code Explanation:

First Create a Project in your favorite Editor you can call it whatever you like. When you are happy with your project folder go to the src directory and create a package if you wish then create a class with main function.

I did : EmailValidation > com.practichouse.validation > EmailValidation
ProjectName > PackageName > ClassName

Now in our case, we need to take input from the user and then we will check that the input from the user is valid or not according to our condition. To take an input we can use Scanner Class or BufferReader with InputStreamReader.

To use BufferReader with InputStreamReader we have to import
import java.io.BufferedReader;
import java.io.InputStreamReader;

To use Scanner we have to import
import java.util.Scanner;

Note: If you want to Scanner please delete BufferedReader and InputStreamReader Or If you want to use them then delete Scanner import.

After this, we will write a function to check that our user input is valid or not. Inside our function, we have to use the regex pattern. If you are not familiar with regex pattern please visit RegEx Official Website

To use RegEx we have to import
import java.util.regex.Matcher;
import java.util.regex.Pattern;

By the way, are checking like this that inside your text there should be a @ symbol and . then com or net something. an output sample is up there.

Thank you so much for reading this post. If you think this one is important for your friends please share with them and keep visiting us for more practices programs and projects to increase your skill level up.

HAPPY CODING…HAPPY LIVING

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jamesraw Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jamesraw
Guest
Jamesraw

Hi, practicehouse.com

I’ve been visiting your website a few times and decided to give you some positive feedback because I find it very useful. Well done.

Thank you for help and I wish you a great week!