1

I need to validate an email pattern which starts with an alphabat and ends with @gmail.com. Following is my code.

 public static boolean isValidEmail(String email)
 {
    String endPattern = "@gmail.com";

    if(null == email){
        return false;
    }   
    if(email.length()<10){
        return false;
    }   
    if(!email.endsWith(endPattern)){
        return false;
    }
    String[] strArr = email.split(endPattern);  
    String mailId = strArr[0];  
    if(!Character.isLetter((mailId.charAt(0)))){
        return false;
    }   
    return true;
  }

Is there a better way to acheive this? A regex or a better code?

Zeeshan
  • 11,851
  • 21
  • 73
  • 98

6 Answers6

4

Use a regex

public static boolean isValidEmail(String email)
{
    if (email != null)
    {
        Pattern p = Pattern.compile("^[A-Za-z].*?@gmail\\.com$");
        Matcher m = p.matcher(email);
        return m.find();
    }
    return false;
}
ToYonos
  • 16,469
  • 2
  • 54
  • 70
1

Use matches method to check for a string whether it starts with an alphabet or not and ends with the string @gmail.com or not

System.out.println("foo@gmail.com".matches("[A-Za-z].*?@gmail\\.com"));
System.out.println("8foo@gmail.com".matches("[A-Za-z].*?@gmail\\.com"));

Output:

true
false
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

According to the Google,

e-mail address

  • should start with a letter or number
  • can only contain letters (a-z), numbers, and periods.
  • should be at least 6 characters

Regex should be

Pattern p = Pattern.compile("^[_A-Za-z0-9]+[\\.[A-Za-z0-9]{5,}@gmail\\.com$");
1
public class Main {

 public static void main(String[] args) {

  String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\
  @([\\w]+\\.)+[\\w]+[\\w]$";
  String email1 = "user@domain.com";
  Boolean b = email1.matches(EMAIL_REGEX);
  System.out.println("is e-mail: "+email1+" :Valid = " + b);
  String email2 = "user^domain.co.in";
  b = email2.matches(EMAIL_REGEX);
  System.out.println("is e-mail: "+email2
  +" :Valid = " + b);
   }
 }
1
package com.mkyong.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

    private Pattern pattern;
    private Matcher matcher;

    private static final String EMAIL_PATTERN = 
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
        + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    public EmailValidator() {
        pattern = Pattern.compile(EMAIL_PATTERN);
    }

    /**
     * Validate hex with regular expression
     * 
     * @param hex
     *            hex for validation
     * @return true valid hex, false invalid hex
     */
    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }
}
Rekha S
  • 73
  • 1
  • 1
  • 11
-1

This is the javascript code

         <script>
  function checkEmail()
      {
     var feedback="";
   var email=document.getElementById("email").value;
  var count=email.length;
  var atpos=email.indexOf('@');
   var atdot=email.lastIndexOf('.');
  var tld=email.substring(atdot);
     if(atpos<1 || atdot<1)
   {
   alert("Email is  Not Valid");
    }
      else if(atpos>atdot)
     {
   alert("Email is  Not Valid");
    }
   else{
   alert("Email is Valid");
       }
      }