-1

I am looking for a universal email validator pattern that can be used in GWTJava. Especially I need to accept emails containing the following characters:

à, ç, é, è, ê, î, ï, ô, ù
Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
Java Learner
  • 311
  • 2
  • 10
  • 26

2 Answers2

2

Those characthers are not valid as per RFC822 or RFC2822 (which obsoletes the first one). They are though part of the draft RFC5335.

Regular expressions to validate emails are a bad idea, generally. They can go as complex as this one. So, usually I try to find a good compromise between result and code complexity. I like to do this:

    try {
        new InternetAddress(email, true);
    } catch (AddressException e) {
        return false;
    }
    

InternetAddress is a class contained in the JavaMail package and the true params tells it to perform a strict validation. It works pretty well. Being it into the official JavaMail package, chances are that the implementation is kept up to date with new releases.

Those charachters are valid in the name of the sender, and this code handles it.

àndreas <andreas@gmail.com> // pass validation
àndreas@gmail.com           // don't pass validation
Community
  • 1
  • 1
namero999
  • 2,882
  • 18
  • 24
  • Yes that solution is the correct, but the beneficts of the expression regular is that you can validate the kind of address that you want, if you use your own email domain it is better to use regular expression but if you use hotmail, yahoo, gmail, etc.. adapted to Rfc is better to use java validation. – Distopic Jul 22 '13 at 10:56
  • 5335 is obsoleted by 6532 since Sep 2012. – BalusC Jul 23 '13 at 13:49
-2
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;

public class ValidatorUtil {

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

/**
 * Validate given email with regular expression.
 * 
 * @param email
 *            email for validation
 * @return true valid email, otherwise false
 */
public static void main(String args[])
{


    String email = "aáaaa@gmail.com";
    // Compiles the given regular expression into a pattern.
    Pattern pattern = Pattern.compile(PATTERN_EMAIL);

    // Match the given input against this pattern
    Matcher matcher = pattern.matcher(email);
    System.out.println(matcher.matches());


}

}
Distopic
  • 717
  • 3
  • 16
  • 31
  • thanks for the answer and can you please tell whether the special characters mentioned above follow RFC 822 standard if you have idea! – Java Learner Jul 22 '13 at 10:05
  • Ufff , i dont have idea about RFC 822 sorry. But a normal validation only accepts number and character included in a-zA-Z0-9 and _- for example in a gmail address if your direction is xxx.yyy@gmail.com and you send a mail to xxxyyy@gmail.com the email arrived to xxx.yyy.... usually only numbers letters and _ - is allowed. – Distopic Jul 22 '13 at 10:08
  • thnaks for the edit Tapas Bose, it is neccesary that you put ^[a-zA-Z0-9_- áéí] if you dont put this it does not works. – Distopic Jul 22 '13 at 10:19
  • itd doesnot work if i use like this: String EMAIL_REGEX = "^[a-zA-Z0-9_-áéí]{2,15}@[a-zA-Z0-9_-áéí]{2,15}.[a-zA-Z]{2,4}(.[a-zA-Z ]{2,4})?$"; boolean lTest = pValue.matches(EMAIL_REGEX); return lTest; – Java Learner Jul 22 '13 at 11:06
  • Because you can not to do that, pass the string to the validator and do the Matcher m=p.matcher(c). – Distopic Jul 22 '13 at 11:11
  • I have passed to validator(Pattern.Compile) and tried but still it does not work!! – Java Learner Jul 22 '13 at 11:15
  • try this : edit in the code above. – Distopic Jul 22 '13 at 11:29
  • I think it's worth mentioning that the only thing of use with GWT (i.e. client-side Java) in this answer is the PATTERN_EMAIL string, used in `String.matches()`. Everything else, including the `main` method is not applicable as GWT [emulates neither `Pattern` nor `Matcher` classes](http://www.gwtproject.org/doc/latest/RefJreEmulation.html). – Boris Brudnoy Jul 22 '13 at 13:35
  • it was only an example jejeje, really i dont know gwt, only a little of regular expression. – Distopic Jul 22 '13 at 13:38