Breaking News
Home / Android / Android Snippets / Email Validator in Android

Email Validator in Android

The snippet shows an Email Validator using RegEx.

  1. package com.livetraxs.app.activity.util;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class EmailValidator {
  7.  
  8.         private Pattern pattern;
  9.         private Matcher matcher;
  10.  
  11.         private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-+]+(.[_A-Za-z0-9-]+)*@"
  12.                         + "[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$";
  13.  
  14.         public EmailValidator() {
  15.                 pattern = Pattern.compile(EMAIL_PATTERN);
  16.         }
  17.  
  18.         /**
  19.          * Validate hex with regular expression
  20.          *
  21.          * @param hex
  22.          *            hex for validation
  23.          * @return true valid hex, false invalid hex
  24.          */
  25.         public boolean validate(final String hex) {
  26.  
  27.                 matcher = pattern.matcher(hex);
  28.                 return matcher.matches();
  29.  
  30.         }
  31. }
[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Advertisment ad adsense adlogger