Search This Blog

Thursday, May 23, 2013

Validate the String for the Email


Here is the method for checking the String is email address or not.


private boolean checkemail(String emailstr) {
// TODO Auto-generated method stub
boolean isValid = false;
   String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
   CharSequence inputStr = emailstr;
   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(inputStr);
   if (matcher.matches()) {
       isValid = true;
   }
   return isValid;

}

For validate the String use following:

if(checkemail(emailstr)){
  // Here do with the Email string
}else{
  System.out.println("email String is not valid");
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.