Menu

How to check valid and invalid email address using Java?

We will see how to validate and check whether entered email ID is valid or not using Java programming. Here I am writing a simple Java code which will validate the format of email using regular expression(regex).

  1. Declare a variable with email addresses of type String[] array.
  2. Write a for loop which will iterate every item(email addresses) from your array.
  3. Create a parameterized method with return type boolean which will accept your email address and check for valid or not.
  4. Method will match the format of your email with regex: [a-zA-Z0-9._-][a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4} and return true or false.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package rashid.jorvee;


public class ValidEmailValidator {

 public static void main(String[] args) {
  String[] emailAddresses= {"sohal@shopoo.co","adyu+@sud.com","asg_sj-@asgd.co","rashid@do.in.com","rashid.ta@rasj.com","fdh%sdfkj_@dsj.cojn"};
  for(String email : emailAddresses) {
   if(validEmail(email)) {
    System.out.println("Valid email address :" +email);
   }
   else {
    System.out.println("Invalid email address :"+email);
   }
  }
 }
 static boolean validEmail(String email) {
  return email.matches("[a-zA-Z0-9._-][a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");
 }
}

Output:

Valid email address :vsohal@shopoo.co

Invalid email address :adyu+@sud.com
Valid email address :asg_sj-@asgd.co
Valid email address :rashid@rasj.com
Valid email address :rashid.ta@rasj.com
Invalid email address :fdh%sdfkj_@dsj.cojn

what is the meaning of color?

Does colors want to say you something and want to convey a message to you? Lets see what is the meaning of a colour which you like most?


Black          : artistic, sensitive, introvert, sophisticated, careful, luxury, sorrow, power, elegance, formality, death, evil, and mystery.



Red          : color of blood, energy, war, danger, strength, excitement, power, determination, passion, desire, and love.



Yellow          : color of shine, cheerful, pleasant, warning, playful, learning and sharing, joy, happiness, intellect, and energy.


White         : color of perfection, light, goodness, innocence, cleanliness, simplicity, purity, and virginity.


Green          : color of nature, growth, harmony, freshness, wealth, prestige and fertility.


Blue          : color of sky, trust, loyalty, wisdom, communicative, calming, confidence, intelligence, faith, truth, and heaven.Purple: color of royalty, power, nobility, luxury, spiritual, majesty, mysterious and ambition.


Orange          : youthful, fresh, enthusiasm, fascination, happiness, creativity, determination, attraction, success, encouragement, joy, and stimulation.


Brown           : organic, wholesome, simple and honest.Pink: Feminine, Sentimental, Romantic and Exciting.

If else || nested if statement in JSTL

Today we will discuss about; how to write simple if else and nested if else statements in JSTL.

If you have to check only a single value then you can use if statement "<c:if>" in JSTL. But if you want to achieve nested if else then you have to use <c:when> and <c:otherwise>.

Will will see both if and nested if else in JSTL with an example.

To use the JSTL in your JSP file we have to first import the JSTL tag library in our JSP file. Below is the import statement to where we are importing JSTL core tag library in our JSP. Specify a prefix to this tag which we will prefix further with our tag.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
prefix="c" use c when you use JSTL tags in your JSP file.
e.g. <c:if>, <c:choose>

Suppose we have a variable name which have a name of an user. let see how will check this using if statement in jstl.
Using <c:set> tag we can declare and set value to a variable. In below statement we are declaring a variable name and assigning its value RASHID.
<c:set var=name value="RASHID">
Now see the if condition 
 <c:if test="${name == 'RASHID'}">
 <c:set var=age value=23 />
</c:if>
Now we will using nested if else how will check the value of variable name in jstl.
<c:choose>
         <c:when test = "${name == 'RASHID'}">
            HIS NAME IS RASHID.
         </c:when>
         <c:when test = "${name == 'Ranjan'}">
            HIS NAME IS Ranjan.
         </c:when>
         <c:otherwise>
            No machtes....
         </c:otherwise>
 </c:choose>
Hope this will help you to understand the if else and nested if else condition using JSTL. Do let me know if you have any question and suggestions.