EmailID validation in JSP using Regular Expressions

Most of the times in the real time applications we will be handling the emailID validation using javascript in the clien side only.

But some times we will be needed to validate the same in the server side.

Below is the code snippet which will demonstrate the EMAILID validation using JSP using regular expressions.


 

//This is the import file which is needed to use the regular expressions
<%@ page import="java.util.regex.*"%>
//This is the well tested Regular expression pattern which can be used to validate any emailID format.
Pattern pt=Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher mt=pt.matcher(emailID);
boolean bl=mt.matches();
if(bl==true)
{
System.out.println("Valid Email ID"+emailID);
}
else
{
System.out.println("Not a Valid Email ID"+emailID);
}

No comments:

Post a Comment